check a string is integer in java code example

Example 1: java test if a string is a int

public static boolean isInt(String str) {
	
  	try {
      	@SuppressWarnings("unused")
    	int x = Integer.parseInt(str);
      	return true; //String is an Integer
	} catch (NumberFormatException e) {
    	return false; //String is not an Integer
	}
  	
}

Example 2: java check if string is number

try {
    int n = Integer.parseInt(str);
} catch (NumberFormatException e) {
    // str is not a number
}

Example 3: how to check if number is integer in java

if(x == (int)x){
 println("x is an integer");
}

Tags:

Java Example