I have wrote a method which takes a string and return true if its a valid single integer or floating number or false if its not.
My code:
public static boolean isDigit(String s)
{
boolean b;
try
{
Integer.parseInt(s);
b = true;
}
catch (NumberFormatException e)
{
b = false;
}
try
{
Double.parseDouble(s);
b = true;
}
catch (NumberFormatException e)
{
b = false;
}
return b;
}
I am sure there is a better way of writing it. Thank you