Converting String to Int

Discussion in 'Plugin Development' started by stormneo7, Dec 8, 2013.

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

    stormneo7

    The purpose of this is to convert a string to an integer so I can later then process it in for loops.

    Say that I had a String made from an args,
    String s = args[0].toString();

    How would I check if s is an integer and how would I convert it to integer form?
     
  2. Offline

    samosaara

    int value = Integer.parseInt(String);
     
  3. Offline

    Developing

    stormneo7 Add this method into your class
    Code:java
    1. public boolean isInteger(String input) {
    2. try {
    3. Integer.parseInt(input);
    4. return true;
    5. }
    6. return false;
    7. }
    8. }

    And simply do this
    Code:java
    1. if(isInteger(s)){
    2. // if string is integer
    3. int value = Integer.parseInt(s);
    4. // converts the string into int.
    5. }else{
    6. // if string is not integer
    7. }
     
    samosaara and CaLxCyMru like this.
  4. Offline

    stormneo7

    Great! Thanks <3
     
Thread Status:
Not open for further replies.

Share This Page