-2

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.

2 Answers2

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;
}
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