为什么我的程序不等待输入?

Why is my program not waiting for input?

我正在尝试获取用户输入并使用正则表达式对其进行验证。但是,当我 运行 程序时,它似乎并没有等待接收任何额外的用户输入。相反,我得到

Exception in thread "main" java.util.NoSuchElementException`

任何有关正在发生的事情的帮助都会很棒!

这是我的代码:

//Retrieve name
System.out.println("What name would you like to search for? (Enter first name only without spaces)");
Scanner input = new Scanner(System.in);
while (!input.hasNext("[A-Za-z]+")) {
    System.out.println("Invalid entry. Please enter first name only.");
    input.next();
}
String name = input.next();
System.out.println("You gave me as input the name:" + name);
}

工作正常。尝试将其作为完全工作的 class(将其另存为 myInput.java,然后 使用 javac myInput.java 编译它 , 然后 运行 它使用 java myInput):

import java.io.*;
import java.util.Scanner;
class myInput {
    public static void main(String[] args) {
    //Retrieve name
    System.out.println("What name would you like to search for? (Enter first name only without spaces)");
    Scanner input = new Scanner(System.in);
    while (!input.hasNext("[A-Za-z]+")) {
        System.out.println("Invalid entry. Please enter first name only.");
        input.next();
    }
    String name = input.next();
    System.out.println("You gave me as input the name:" + name);
    }
}

OUTPUT(我先给出一个无效的输入(test1)然后正确打印一个有效的(test)):

test1
Invalid entry. Please enter first name only.
test
You gave me as input the name:test