[英]How can I repeatedly read user input using Scanner java
I am trying to create a simple menu for my program that reads user input. 我正在尝试为我的程序创建一个简单的菜单来读取用户输入。 Here's the code: 这是代码:
public void menu() {
String command;
System.out.println("To operate with words write: a");
System.out.println("To operate with products write: b");
System.out.print("\nGive the command: ");
Scanner scanner = new Scanner(System.in);
command = scanner.nextLine();
if (command.equals("a")) {
System.out.println("\nGood job! You have chosen the word program\n");
System.out.println("You can only use the following commands: ");
System.out.println("exit - to exit the program");
System.out.println("add - to add a word into the map");
System.out.println("lookup - to lookup a word into the map\n");
System.out.print("Give a command: ");
command = scanner.next();
while(scanner.hasNext()){
command = scanner.next();
if(command.equals("add")){
this.addWord();
}
System.out.print("Give a command: ");
command = scanner.next(); //Here I get the ERROR that throws Exception
}
}
scanner.close();
}
public static void main(String [] args)
{
HashTableInterface table = new HashTable();
Dictionary d = new Dictionary(table);
RepositoryInterface repo = new Repository(d);
ControllerInterface controller = new Controller(repo);
Console c = new Console(controller);
c.menu()
}
And here's a test with the exception I get: 这是我得到的例外测试:
To operate with words write: a
To operate with products write: b
Give the command: a
Good job! You have chosen the word program
You can only use the following commands:
exit - to exit the program
add - to add a word into the map
lookup - to lookup a word into the map
Give a command: add
add
Give the word: mar
Give the word's plural: mere
The hash code for mar is: 3
Give a command: Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at console.Console.menu(Console.java:56)
at test.Prog.main(Prog.java:26)
I removed the second command = scanner.next() from the while loop but then it let's me read user input only once. 我从while循环中删除了第二个命令= Scanner.next(),但是让我仅读取一次用户输入。 How can solve this? 怎么解决呢?
UPDATE: the AddWord method: 更新:AddWord方法:
public void addWord() {
Scanner scanner = new Scanner(new InputStreamReader(System.in));
System.out.print("Give the word: ");
String word = scanner.nextLine();
System.out.print("Give the word's plural: ");
String plural = scanner.nextLine();
scanner.close();
controller.addTheWord(word, plural);
}
You should use switch case
inside the while
loop instead of if
. 您应该在while
循环中使用switch case
while
不是if
。 The switch case should provide the functionality depending on the input provided by the user. 开关盒应根据用户提供的输入来提供功能。
You can use 您可以使用
InputStreamReader str= new InputStreamReader (System.in);
BufferedReader uinp= new BufferedReader (str);
String val;
val= uinp.readLine();
while (!".".equals(val)) {
//do your stuff
//..and after done, read the next line of the user input.
val= uinp.readLine();
}
Why not do something like this: 为什么不做这样的事情:
public class test {
private static Scanner scanner;
public static void main(String args[]) {
scanner = new Scanner(System.in);
printmenu();
getinput();
}
private static void printmenu(){
System.out.println("To operate with words write: a");
System.out.println("To operate with products write: b");
}
private static void getinput(){
System.out.print("\nGive the command: ");
String command = "";
while (!scanner.hasNext()) {
}
command = scanner.next();
if (command.equals("a")) {
System.out.println("\nGood job! You have chosen the word program\n");
System.out.println("You can only use the following commands: ");
System.out.println("exit - to exit the program");
System.out.println("add - to add a word into the map");
System.out.println("lookup - to lookup a word into the map\n");
System.out.print("Give a command: ");
while(scanner.hasNext()){
command = scanner.next();
if(command.contains("add")){
addWord();
}
else{
System.out.println(command);
}
getinput();
}
}
else{
scanner.close();
}
}
public static void addWord() {
System.out.print("Give the word: ");
String word = scanner.next();
System.out.print("Give the word's plural: ");
String plural = scanner.next();
//controller.addTheWord(word, plural);
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.