Checks whether the String contains only digit characters in Java
Description
The following code shows how to checks whether the String contains only digit characters.
Example
//from w w w .ja va 2 s.com
public class Main {
public static boolean isDigits(String str) {
if (isEmpty(str)) {
return false;
}
for (int i = 0; i < str.length(); i++) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isEmpty(String str) {
return str == null || str.length() == 0;
}
public static void main(String[] argv){
System.out.println(isDigits("java2s.com") );
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »