How to check whether a string contains numeric or not?? Example: String name = "Annie Joseph158" Note: String should contain only alphabets. Now i want to check String "name" whether it consists of numbers or not? The String "name" consists of both firstname and lastname. If either firstname or lastname consist any numbers then appropriate error should be displayed that "Invalid name!! Name should not consist of Numbers". I used Regex(pattern) concept but its only for one name (not for both firstname and lastname). Could anyone please write a code for me?? Thanks in advance.
Asked
Active
Viewed 95 times
-2
-
Can you post your code here – newstackoverflowuser5555 May 16 '15 at 04:46
-
1"Could anyone please write a code for me" definitely not. The process is: You write code, we help fix it. Get to writing. – Kon May 16 '15 at 04:47
-
possible duplicate of [How to check if a String is a numeric type in Java](http://stackoverflow.com/questions/1102891/how-to-check-if-a-string-is-a-numeric-type-in-java) – Sami Kuhmonen May 16 '15 at 04:49
2 Answers
1
You can try in this way
boolean validateFirstName = isValidString(firstname);
boolean validateLastName = isValidString(lastname);
public boolean isValidString(name){
for(char chr : name.toCharArray()){
if(Character.isDigit(chr)){
return false;
}
}
return true;
}

newstackoverflowuser5555
- 741
- 2
- 11
- 31
0
public static void main(String args[]) {
String a = "YOUR STRING";
String[] arr = a.split("[0-9]");
if(arr.length == 0 || arr.length > 1) {
System.out.println("Invalid name!! Name should not consist of Numbers");
}
}

Soumitri Pattnaik
- 3,246
- 4
- 24
- 42