[solved]getting an int from a string?

Discussion in 'Plugin Development' started by tommycake50, Sep 5, 2012.

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

    tommycake50

    So if a player types /mycommand args
    and the args contain an int rather then a string.
    how would i use this like an int say if i wanted to do args[0] + args[2]?
     
  2. Offline

    Firefly

    Integer.parseInt(someString) must be surrounded with a try/catch
     
  3. Offline

    Timr

    You could do something like this using Integer.parseInt(String)

    Code:java
    1.  
    2. int a = 0;
    3. int b = 0;
    4. try {
    5. a = Integer.parseInt(args[0]);
    6. b = Integer.parseInt(args[1]);
    7. } catch(NumberFormatException ex) {}
    8.  
    9.  
    10. System.out.println(a + b);


    Sniped by Firefly
     
  4. Offline

    Firefly

    ;)
     
  5. Offline

    tommycake50

    ok so i made a new variable and added them together how would i send the answer variable as a string to a player?
     
  6. tommycake50

    Code:
    int result = a + b;
    player.sendMessage(result);
    
     
  7. Offline

    tommycake50

    yeah that's what i did xD it says The method sendMessage(String) in the type CommandSender is not applicable for the arguments (int)
     
  8. String.valueOf(result);
     
  9. Offline

    Delocaz

    I prefer something like 1024+"". Just add "" to the int and it'll be a string.
     
  10. Offline

    Sushi

    Note that you do not need to surround the method in try/catch blocks, but it is advised unless you know that whatever you are converting to an integer consists of an integer every time.
     
  11. Offline

    tommycake50

    thanks everyone for the help.
     
Thread Status:
Not open for further replies.

Share This Page