Solved java.lang.NumberFormatException: For input string: "Game"

Discussion in 'Plugin Development' started by leet4044, Aug 24, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    leet4044

    So, I'm having this error, and I need some help to resolve it.
    So what should happen, is the command should set the lobby for the game, when a player joins it just displays that error, and takes them to the void. Help please? :]

    Error: http://pastebin.com/2RGTJuds

    Code for the command:
    Code:java
    1. @Override
    2. public boolean onCommand(Player player, String[] args) {
    3. if(player.isOp()){
    4. if(args.length == 1){
    5. int i = Integer.parseInt(args[0]);
    6. SettingsManager.getInstance().setGameLobbySpawn(i, player.getLocation());
    7. }
    8.  
    9.  
    10. }
    11. return true;
    12. }
     
  2. Offline

    Stoux

    Well clearly: "Game" is not a number. So it throws a NumberFormatException because Integer.parseInt(s) fails to parse the String. So I'm not sure what the command is, that is being used, but something is clearly not being entered right.
    What is the command?/What do you epect args[0] to be?

    Also, if you're planning to make an int of an argument the player can enter, always check if it's actually a number. You can do this easily with a try {} catch() {} system:
    Code:java
    1. if (args.length == 1) {
    2. try {
    3. int i = Integer.parseInt(args[0]);
    4. //Rest of the code here
    5. } catch (NumberFormatException ex) {
    6. //Player did the command wrong
    7. return false;
    8. }
    9. }
     
    leet4044 likes this.
  3. Offline

    xTrollxDudex

    leet4044
    You can't override a method that doesn't exist.
     
    leet4044 likes this.
  4. Offline

    leet4044

    The command is suppose to set an X Y Z where the player is standing in the system file, then save it. Wich is in the commands class:
    Code:java
    1. public void setGameLobbySpawn(int gameid, Location l) {
    2. spawns.set("spawns." + gameid + ".lobby.world", l.getWorld().getName());
    3. spawns.set("spawns." + gameid + ".lobby.x", l.getBlockX());
    4. spawns.set("spawns." + gameid + ".lobby.y", l.getBlockY());
    5. spawns.set("spawns." + gameid + ".lobby.z", l.getBlockZ());
    6. saveSpawns();
    7. }
     
  5. Offline

    Stoux

    What.. Not sure if troll or not

    Ok, then the command should be something like "/setspawn [gameid]" right? Use the snippit of code I posted to check if [gameid] is an actual number :)
     
    leet4044 likes this.
  6. Offline

    Tirelessly

    How do you know that that method doesn't exist? He's probably using a player command superclass and only passing the info he needs.
     
    leet4044 likes this.
  7. Offline

    leet4044

    Stoux
    Console:
    [INFO] Young_Explicit issued server command: /castleraiders setlobby Game
    [INFO] args = [Game]
    [INFO] Young_Explicit issued server command: /castleraiders setlobby game
    [INFO] args = [game]
    Before I compiled, I made the code:
    Code:java
    1. public boolean onCommand(Player player, String[] args) {
    2. if(player.isOp()){
    3. System.out.println( "args = " + java.util.Arrays.toString( args ) );
    4. if(args.length == 1){
    5. try {
    6. int i = Integer.parseInt(args[0]);
    7. SettingsManager.getInstance().setGameLobbySpawn(i, player.getLocation());
    8.  
    9. } catch (NumberFormatException ex) {
    10. player.sendMessage(ChatColor.RED + "You did this command wrong.");
    11. return false;
    12. }
    13. }
    14. }
    15. return false;
    16. }
    17.  
     
  8. Offline

    metalhedd

    leet4044

    you're doing:

    int i = Integer.parseInt(args[0]);

    this is trying to take the first argument and interpret it as a number. but the first argument is the string 'Game'. so it crashes.

    What are you trying to do with that line of code?
     
    leet4044 likes this.
  9. Offline

    xTrollxDudex

    Tirelessly Stoux
    Worded wrong. You can't use an @Override on a method that... well, there comes the part that is difficult to understand, he doesn't pass all the parameters, therefore he should not need an @Override.
     
    leet4044 likes this.
  10. Offline

    metalhedd


    While this is PROBABLY correct in most cases, and definitely a safe assumption to make.. you have to throw it all out the window because of the exception he's getting. It wouldn't get that far if that was the case. so he is obviously subclassing something which does have that method.
     
    leet4044 likes this.
  11. Offline

    leet4044

    There's 2 commands, /castleraiders setlobby spawn
    and /castleraiders setlobby game
    Neither are working.
    This class that the int i = Integer.parseInt(args[0]); is in, is the class for /castleraiders setlobby game
    Btw, both of these commands are sub commands.

    /Castleraiders setlobby game:
    Code:java
    1. public void setGameLobbySpawn(int gameid, Location l) {
    2. spawns.set("spawns." + gameid + ".lobby.world", l.getWorld().getName());
    3. spawns.set("spawns." + gameid + ".lobby.x", l.getBlockX());
    4. spawns.set("spawns." + gameid + ".lobby.y", l.getBlockY());
    5. spawns.set("spawns." + gameid + ".lobby.z", l.getBlockZ());
    6. saveSpawns();
    7. }


    /Castleraiders setlobby spawn:
    Code:java
    1. public void setLobbySpawn(Location l) {
    2. system.set("system.lobby.spawn.world", l.getWorld().getName());
    3. system.set("system.lobby.spawn.x", l.getBlockX());
    4. system.set("system.lobby.spawn.y", l.getBlockY());
    5. system.set("system.lobby.spawn.z", l.getBlockZ());
    6. saveSystemConfig();
    7. }



    This is the class file for the setlobby spawn code:
    Code:java
    1. @Override
    2. public boolean onCommand(Player player, String[] args) {
    3. if(player.isOp()){
    4. SettingsManager.getInstance().setLobbySpawn(player.getLocation());
    5. }
    6. return true;
    7. }
    8.  


    This is the class file for the setlobby game code:
    Code:java
    1. @Override
    2. public boolean onCommand(Player player, String[] args) {
    3. if(player.isOp()){
    4. System.out.println( "args = " + java.util.Arrays.toString( args ) );
    5. if(args.length == 1){
    6. try {
    7. int i = Integer.parseInt(args[0]);
    8. SettingsManager.getInstance().setGameLobbySpawn(i, player.getLocation());
    9.  
    10. } catch (NumberFormatException ex) {
    11. player.sendMessage(ChatColor.RED + "You did this command wrong.");
    12. return false;
    13. }
    14. }
    15. }
    16. return false;
    17. }
    18.  
     
  12. Offline

    metalhedd

    leet4044 the problem is that "Game" is not a number clearly.. but your command expects args[0] to be a number. (the game id presumably) so why aren't you calling the command with a number?
     
    leet4044 likes this.
  13. Offline

    leet4044

    metalhedd Never have done that before, I'm fairly new to java.
     
  14. Offline

    metalhedd

    You're not undeerstanding my question.. I'm suggesting that you dont HAVE A programming problem or bug.. you just aren't passing the right arguments to your command. it wants a number, not the word 'Game'.=
     
    leet4044 likes this.
  15. Offline

    leet4044

    Nevermind about what I just said, it works like it should! thanks to all who replied!
     
Thread Status:
Not open for further replies.

Share This Page