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

You can use the split method (String class) to split your input into an array of words, then check how many words there are, then check each word using isLetter

You can use the split method (String class) to split your input into an array of words, then check how many words there are, then check each word using isLetter

Once I split the string into an array of words, and perform the check of how many words there are, how do I check each word using isLetter? Would I need a for loop to run through each of the words?

Would I need a for loop to run through each of the words?

Yes. So that's two loops - one loops through the (up to 3) words, then a loop inside that loops thru the letters of the word.

Yes. So that's two loops - one loops through the (up to 3) words, then a loop inside that loops thru the letters of the word.

I think I've nearly got it now. The only problem is that the compiler throws an exception if the name is less than three words in length. The rest of the method seems to work correctly.

public static boolean nameOk(String x){
       Scanner sc = new Scanner(x);
       String[] words = x.split (" ");
       boolean alphaOk = true;
       
       for(int i=0;i<3;i++){
           for(int j = 0;j<words[i].length();j++){
               if(Character.isLetter(words[i].charAt(j))==false){
                  alphaOk = false;
               }
           }
       }
       System.out.println(alphaOk);
       if(words.length<=3&&alphaOk==true)return true;
       else return false;
     }

I'm not sure where the problem is in the two for statements. If I enter three or more words it works fine (more than 3 rejects the name and asks for another one), but anything less throws an exception.

line 6 instead of using 3 as the upper limit of your loop you should use the actual length of the words array. (It's like, but not the same syntax as, line 7, where you use the actual length of the String as the upper limit for the loop)

Comments
Helped solve the problem!
This question has already been answered. Start a new discussion instead.