Question I can't make Args[1] to convert to a int

Discussion in 'Plugin Help/Development/Requests' started by ThatOneChickenJockey, Jul 18, 2016.

Thread Status:
Not open for further replies.
  1. I am attempting to make a hub plugin for a friend and I am trying to make this command /setup playersperhub <int> the int is args[1] and it comes out as a string, meaning it cannot be converted to a int for purposes of saving that number to the config.yml file. How do I make that arg come out as a int?
     
  2. Offline

    thapengwin

    Integer.parseInt
     
  3. So I would use it like this then:
    Int int = Integer.parseInt(args[1]);?
     
  4. Offline

    I Al Istannen

    @ThatOneChickenJockey
    if you wanted to write
    "Intger integer = Integer.parseInt(args[1]);"
    or
    "int integer = Integer.parseInt(args[1]);"

    Yes. Just use a try block to catch the NumberFormatException. This will get thrown if the String (args[1]) is no valid integer.
     
  5. So do this:

    try {
    int int = Integer.parseInt(args[1]);
    } catch (NumberFormatExeption ex) {
    player.sendMessage(ChatColor.RED + """ + args[1] + "" is invalid for type "Int"");
    }


    right?
     
  6. Offline

    I Al Istannen

    @ThatOneChickenJockey
    "int int" won't work, as "int" is a reserved word and not accepted for a variable name. And you maybe want to return in the catch block, to stop further code execution.

    But apart from that, yes :) It seems to be correct.

    [code=java]<your code>[/code] will make your code look a lot better, just try it ;)
     
  7. Or just assign to int to 0 or something like that, it's what I usually do, that way the command still works even if they put an invalid number.
     
Thread Status:
Not open for further replies.

Share This Page