如何使用Scanner java重复读取用户输入
我正在尝试为我的程序创建一个简单的菜单来读取用户输入。代码如下:如何使用Scanner java重复读取用户输入,java,input,Java,Input,我正在尝试为我的程序创建一个简单的菜单来读取用户输入。代码如下: 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 comman
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()
}
下面是一个测试,我得到的一个例外:
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)
我从while循环中删除了第二个命令=scanner.next(),但它只允许我读取用户输入一次。如何解决这个问题
更新: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);
}
您应该在
while
循环中使用开关盒,而不是if
。开关盒应根据用户提供的输入提供功能 您可以使用
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();
}
为什么不这样做:
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);
}
}请显示您的主界面。@AliAlamiri这是我的主界面:HashTableInterface table=new HashTable();字典d=新字典(表);RepositoryInterface repo=新存储库(d);控制器接口控制器=新控制器(回购);控制台c=新控制台(控制器);c.菜单();你能发布你的addWord()方法吗?是使用nextLine()读取用户输入吗?@CodingBird使用该方法更新了帖子您是从文本文件获取用户输入吗?引发NoTouchElementException的一个可能原因是,您试图读取已到达数据末尾的输入,直到并且除非您给出“.”,否则它会继续请求用户输入。@实际上,我们看不到您的屏幕。请告诉我们,例外情况是什么。“我有一个例外”几乎没有有用的信息。@Fildor抱歉!这里有一个例外:java.io.IOException:Stream在sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)在sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)在java.io.BufferedInputStream.GetBufiOpen(BufferedInputStream.java:162)在java.io.BufferedInputStream.read(BufferedInputStreamDecoder.java:325)在java在java.io.InputStreamReader.read(InputStreamReader.java:184)在java.io.BufferedReader.fill(BufferedReader.java:154)在java.io.BufferedReader.readLine(BufferedReader.BufferedReader)在java.io.BufferedReader.readLine(BufferedReader.BufferedReader)在java.io.InputStreamReader.read(InputStreamReader.java:184)在java.io.io.io.BufferedReader,现在试一试。@BharathRallapalli拼写错误不是问题所在。我正在使用Eclipse,它会告诉我是否存在与问题无关的书写错误。@ViliamBúr请详细说明。将if
块替换为switch case
块的好处是,如果多个分支测试同一个表达式,则表达式n只计算一次。在这种特定情况下,表达式是command.equals(“add”)
,并且在循环中只计算一次。对字符串值重复调用equals
方法可能效率低下,但它不会引发NoSChelementException,这就是问题所在。