Adding args[1] to an int variable

Discussion in 'Plugin Development' started by DutchChris, Jul 14, 2020.

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

    DutchChris

    Hello!

    I recently wanted to make a money-like plugin and was working on a command that allows you to store a certain amount of money to your balance. I wanted to add args[1] of my /store command onto my balance variable, so adding a string to an integer, but I have no clue how I would have to do this. Any suggestions?

    Thanks in advance!
     
  2. Offline

    CraftCreeper6

  3. Offline

    DutchChris

    So lets say I use /store 10 and I want to add the 10 to my balance integer, how would I then add it using parseInt? It keeps giving me the error that I can't add args[1] because it's a string even after using a parseInt function.
     
  4. Offline

    Plootonix

    @DutchChris Example:
    Code:
    Integer.parseInt(args[1])
     
  5. Offline

    Strahan

    Don't forget to trap errors in case someone fat fingers it and passes something non numeric.
     
  6. Offline

    DutchChris

    Sorry, I'm still very new to programming, but how would I then make it so the String args[1] is recognized as an int? I tried making a simple function like this:

    public static int toInt(String p) {
    Integer.parseInt(p);
    return p;
    }

    but it doesn't see p as an int but instead still as a string.

    I really appreciate the help!
     
  7. Offline

    timtower Moderator Moderator

    @DutchChris You need to return the Integer.parseInt(p)
    Not the p
     
  8. Offline

    DutchChris

    Ah that makes sense. I now have this line:

    balance += Integer.parseInt(args[1]);

    but it gives me an internal error when I run the command with this piece of code in it.

    Sorry for the amount of questions, hope you don't mind
     
  9. Offline

    timtower Moderator Moderator

    What error then? ArrayOutOfBounds? CastException?
     
Thread Status:
Not open for further replies.

Share This Page