How to check a string contains only int in JAVA?

java

Solution

You can use Integer.parseInt(integerString);

public boolean isInteger(String integerString){

   try{
      Integer.parseInt(integerString);
      return true;
   } catch (NumberFormatException nfe) {
      return false;
   }
}

a NumberFormatException means parsing will not be successful hence the String is not an integer.

Problem

I hope to write a small method to do the following things: For example, the string a = "a123", then the method should return fulse; the string b = "111", it should return true. It means only the string is an int, it should return true, all the other cases should return false. Does anyone can help me? Thank you!

Original source

Related problems