I don't understand how to check for non-alpha characters in a string that has more than one word. I'm basically asking for a retype if the string has more than 3 words or if it has non-alpha characters. I'm wondering if Character.isLetter is not what I'm looking for since it throws a false as soon as it hits the first space.
Scanner sc = new Scanner(System.in);
while(true){
System.out.print("Enter Name(enter blank line when done): ");
String name = sc.nextLine();
while (nameOk(name)==false){
System.out.print("Enter another name(enter blank line when done): ");
name = sc.nextLine();
}
if(name.length() == 0) break;
System.out.println("Hello " + name);
}
System.out.println("Goodbye");
}
public static boolean nameOk(String x){
Scanner sc = new Scanner(x);
int numOfHunks = 0;
while(sc.hasNext()){
numOfHunks++;
sc.next();
}
for (int i = 0;i<x.length();i++){
if(Character.isLetter(x.charAt(i))==false){
}
}
if(numOfHunks<=3){
return true;
}
else{
return false;
}
}