I have three text fields all which are numbers or a decimal number.
They are accepted as strings which are later converted.
However, i want to validate these fields so that only the numbers or decimal numbers are accepted.
Whats the easiest way to ensure that these fields are only characters 0-9.
I would prefer an if statement but any solution would be nice!
Using isDigit() and all() Here, we check if each character of a String is a numeric digit. The all method returns true if all characters return true when passed to the isDigit method.
Use the isDigit() Method to Check if String Contains Numbers in Java. To find an integer from a string, we can use this in-built function called isDigit() .
Use string.matches
method which accepts a regex as an argument.
if(string.matches("\\d+(?:\\.\\d+)?"))
{
System.out.println("Matches");
}
else
{
System.out.println("No Match");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With