'Scanner doesn't update input when returning to method Java

I'm writing a word-guessing game code. The main calls the inputTake method, which asks for input of a word consisting 5 English letters only, and returns is. Before returning the word, it calls another method, checkInput, to make sure the input is valid. If the input isn't valid, the checkInput method prints an error message and calls inputTake to let the user try again.

But when the first input is invalid, checkInput calls inputTake and then the second input is valid everything seems to work alright. The problem is that the method returns the first, invalid input, and not the valid input.

I tried initializing Scanner in the main and giving it to the method as parameter, but that doesn't help.

Below is the code I wrote, any thoughts? Any help is welcome


Main:

Board board1 = new Board();
        
String guess = board1.inputTake();

Board:

// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
    Scanner scan = new Scanner(System.in);
    String guess;

    System.out.println("choose a word, pick carefully: ");
    guess = scan.next();
    
    // we gotta check whether the input's valid before we return it!
    checkInput(guess);
        
    return guess;
    }
    
    /* checks whether a given String is made out of 5 english language letters. 
     * if it is, program continues normally.
     * if not, it prints error message and calls the InputTake method again.
     */
public void checkInput(String input) {
    boolean isGood = true;
        
    // check if 5 letters
    if(input.length() != 5)
        isGood = false;
        
    // check if all are english
    if(!input.matches("[a-zA-Z]+")) 
          isGood = false;
        
    if(isGood == false) {
        System.out.println("make sure your guess consists of 5 english letters, try again.");
        inputTake();
    }
}


Solution 1:[1]

As mentioned in the comments, the problem is that your inputTake() call inside checkInput() doesn't do what you want. You can try this:

// take input - print a message and calls the checkInput method with the String inputed.
public String inputTake(){
    Scanner scan = new Scanner(System.in);
    String guess;

    System.out.println("choose a word, pick carefully: ");
    guess = scan.next();
        
    // we gotta check whether the input's valid before we return it!
    if(!isGoodInput(guess)) {
        System.out.println("make sure your guess consists of 5 english letters, try again.");
        guess = inputTake();
    }
    return guess;
}
        
/* checks whether a given String is made out of 5 english language letters. 
* if it is, program continues normally.
*/
public boolean isGoodInput(String input) {
    return input.length() == 5 && input.matches("[a-zA-Z]+");
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 happy songs