Answers
Apr 25, 2007 - 12:33 PM
eg
try {
num = Integer.parseInt ( txtbox.getText());
} catch (NumberFormatException E) {
// value entered in txtbox is not numeric
}
Sep 22, 2010 - 03:23 AM
Apr 27, 2011 - 05:35 AM
/**
* This method checks if a String contains only numbers
*/
public boolean containsOnlyNumbers(String str) {
//It can't contain only numbers if it's null or empty...
if (str == null || str.length() == 0)
return false;
for (int i = 0; i < str.length(); i++) {
//If we find a non-digit character we return false.
if (!Character.isDigit(str.charAt(i)))
return false;
}
return true;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
System.out.println(main.containsOnlyNumbers("123456"));
System.out.println(main.containsOnlyNumbers("123abc456"));
}
}
Add New Comment