Validate if string is an integer in Java
Description
The following code shows how to validate if string is an integer.
Example
// w w w. j a va 2s . co m
public class Main {
public static boolean checkIfNumber(String in) {
try {
Integer.parseInt(in);
} catch (NumberFormatException ex) {
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(checkIfNumber("ABC"));
System.out.println(checkIfNumber("123123123123123"));
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »