String idText = "12";
int id = null;
try {
id = Integer.parseInt(idText);
}
catch (NumberFormatException nfe) {
// If Exception then it is not an int.
// Do Error Handling.
}
// If no exception then it is an int
System.out.println(id);
thanks. That was more or less what I was doing except I wasn't using the NumberFormatException. I thought java would have a function to test this like VB, it seems I was wrong
There is also a isDigit() method but it is only for characters. Alternative solution would be (but i'd prefer use that NumberFormatException catching):
String s = "12";
if(isdigit(s))
{
System.out.println(s + " is digit."
}
else
{
System.out.println(s + " is not digit."
}
private boolean isdigit(String str_)
{
char[] c = str_.toCharArray();
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.