Plugin Help How do I check if an arg is an int

Discussion in 'Plugin Help/Development/Requests' started by ObviousEmeralds, Nov 21, 2015.

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

    ObviousEmeralds

    Like the title says, How do I check if an arg is an int? I've looked it up and all I could find was this
    Code:
    
    public static boolean isInt(String string) {
    try {
    Integer.parseInt(string);
    } catch (NumberFormatException nFE) {
    return false;
    }
    return true;
    }
    
    
    Whenever I do this I get an error
    [​IMG]
    Could anyone help? :p

    Thanks!

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 22, 2015
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    sgavster

    It's because it isn't final that you're getting that error
     
  4. Offline

    ObviousEmeralds

  5. Offline

    timtower Administrator Administrator Moderator

  6. Offline

    ObviousEmeralds

    @timtower If I click it I get another error (It says the same thing on the other side but with a backwards bracket)
    [​IMG]
    If I click the error I add brackets to the front and back of the first line I get this:
    [​IMG]
     
  7. Offline

    timtower Administrator Administrator Moderator

  8. Offline

    Lolmewn

    The method itself is fine, there's probably something wrong with the code above it, causing it to not be able to parse your method.
     
  9. Offline

    causticlasagne

    I'm not having any issues using this.
    Try this:

    boolean isInt (String s)
    {
    Integer i = Integer.parseInt(s);
    return false;
    }

    I'm surprised there is no tryParse function for Integer.
     
  10. Offline

    Firestar311

    In your Parameters, the String needs to be final, so it would look like this;
    Code:
    public static boolean isInt(final String string) {
          try {
          Integer i = Integer.parseInt(string);
          } catch (NumberFormatException e) {
          return false;
         }
         return true;
       }
    
    What this does is that it tries toe parse the int, if it cannot, it will catch the exception and return false, if it succeeds, it will return true
     
    Last edited: Dec 2, 2015
  11. Offline

    teej107

    No it doesn't.

    Your code is the exact same as the OPs except with a syntax error.
     
  12. Offline

    Firestar311

    Ok, fixed the errors, I was doing it fast and did not see the errors, my bad.
    Mine is different because I put "Integer i = Integer.parseInt(string);" in my code. Although testing both mine and his, I got no errors either way.

    @ObviousEmeralds I have tested both codes in my Development enviroment and have found no errors, I do not get that final error either. Could you send the full code please via pastebin.com?
     
  13. Offline

    Mrs. bwfctower

  14. Offline

    Firestar311

  15. Offline

    Mrs. bwfctower

    @Jayfeather331 it's packaged with Bukkit
     
Thread Status:
Not open for further replies.

Share This Page