Searching easy way to check if a String contains pure numeric...
Hi !
Im searching for an easy way to check if a String contains
- a pure numeric value or
- a decimal value or
- a text.
I know the method to convert the Srting in the type char and to check every character, but is there an easy methode to check the String direct?
Thank you Wolfgang
Hi
i know what you are talking about but he fastest and the easiest way is stillthe following.
int i = 0;
if ("1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM".indexOf(yourString.charAt(i)) == -1)
{
// If the character does not match up with any of the above it will enter the if statement.
// Do what ever you want to do.
}
Ciao
Here ya go:
String s = whatever;
try {
int temp = Integer.parseInt(s);
System.out.println("It is an integer");
}
catch (Exception e) {
try {
Double temp = Double.valueOf(s);
System.out.println("It is a decimal");
}
catch (Exception e) {
System.out.println("It is not a number");
}
}
Of course, you will want to replace the prints with whatever you want.
GN
Here is a little method that I use to check a string to see if it is all digits.
public static boolean isStringAllDigits(String aString) {
int stringSize = aString.length();
for (int i = 0; i < stringSize; i++){
boolean check = Character.isDigit(aString.charAt(i));
if (!check) {
return false;
}
};
return true;
}
You could modify the code to also check to see if the character is a letter by using isLetter(). You can check for decimals by using the string indexOf()method, i.e.
int position = someString.indexOf(".");
if position equals -1 then the decimal was not found.
I hope this helps.
Robert