java 尝试获取输入时没有此类元素异常

java No Such Element Exception when trying to get input

我试图在循环中获取用户输入,但它在第二次迭代时崩溃了。我获得了一次输入,但在第二次调用 getInput() 时崩溃了,我知道第一次迭代已完全完成。

while(!isAnagram(playerLetters,cw.toString())) {
        char userChar = getInput(); //crash on the second call
        System.out.println(userChar); //for debugging
        if(cw.toString().indexOf(userChar) == -1) 
            System.out.println("Character not in the word, try again");
        else {
            for(int i = 0; i < cw.numOfOccurrences(userChar); i++)//add the letter to correct guessed letter the number of time it occurs in the word 
                playerLetters = playerLetters + userChar;
            System.out.println("The character is in the word!");
        }
        System.out.println(userChar);   //for debugging
        printUI();
        trys++;
    }
    

这是getInput():

private char getInput() {
    Scanner scan = new Scanner(System.in);
    String current = ""; 
    
    do {
        current = scan.next();
        if(current.length() > 1) 
            System.out.println("Please enter only one character");
        
        else if(usedLetters.contains(current.charAt(0))) 
            System.out.println("This character has already been used");
        
        else if(!Character.isLetter(current.charAt(0)) || !Character.isLowerCase(current.charAt(0)))  //Check if input is a lower case letter
            System.out.println("Invalid input");
        else {
            usedLetters.add(current.charAt(0));
            break;  
        }
    } while(true);
    
    scan.close();
    return current.charAt(0);

}

你可以放

Scanner scan = new Scanner(System.in)

在 getInput 方法之外不要在 class

中的每次迭代中调用它
Scanner scan = new Scanner(System.in);

private char getInput() {
   
    String current = ""; 
    
    do {
        current = scan.next();
        if(current.length() > 1) 
            System.out.println("Please enter only one character");
        
        else if(usedLetters.contains(current.charAt(0))) 
            System.out.println("This character has already been used");
        
        else if(!Character.isLetter(current.charAt(0)) || !Character.isLowerCase(current.charAt(0)))  //Check if input is a lower case letter
            System.out.println("Invalid input");
        else {
            usedLetters.add(current.charAt(0));
            break;  
        }
    } while(true);
    
   
    return current.charAt(0);

}

....
// then your loop 

while(!isAnagram(playerLetters,cw.toString())) {
        char userChar = getInput(); //crash on the second call
        System.out.println(userChar); //for debugging
        if(cw.toString().indexOf(userChar) == -1) 
            System.out.println("Character not in the word, try again");
        else {
            for(int i = 0; i < cw.numOfOccurrences(userChar); i++)//add the letter to correct guessed letter the number of time it occurs in the word 
                playerLetters = playerLetters + userChar;
            System.out.println("The character is in the word!");
        }
        System.out.println(userChar);   //for debugging
        printUI();
        trys++;
    }

// dont forget to close your scanner after the loop
 scan.close();