我正在尝试为我的程序创建一个简单的菜单来读取用户输入.这是代码:

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("

Give the command: ");

Scanner scanner = new Scanner(System.in);

command = scanner.nextLine();

if (command.equals("a")) {

System.out.println("

Good job! You have chosen the word program

");

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

");

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);

}

scanner java重新输入_如何使用Scanner Java重复读取用户输入相关推荐

  1. java如何重复userinput_如何使用Scanner Java重复读取用户输入

    我正在尝试为我的程序创建一个简单的菜单来读取用户输入.这是代码: public void menu() { String command; System.out.println("To op ...

  2. python无限次输入_在Raspberry Pi上的Python用户输入无限循环内部输入时很多输入错过了输入...

    您可能应该使用类似于以下的代码直接从stdin读取: import os import sys import select stdin_fd = sys.stdin.fileno() try: whi ...

  3. JAVA同时输入用户名和密码_用java模拟设计一个简单的“用户注册”程序。当用户输入用户名和密码时,单击“注...

    用java模拟设计一个简单的"用户注册"程序.当用户输入用户名和密码时,单击"注 2020 - 9 - 26 TAG : 所有功能均已实现,如有不满意的地方我再修改imp ...

  4. Java黑皮书课后题第8章:**8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府。当接收到用户输入后,程序报告答案是否正确。假设50个州以及它们的首府保存在一个二维数组中,提示用户回答所

    **8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府 题目 题目描述与运行示例 破题 代码 题目 题目描述与运行示例 **8.37(猜测首府)编写一个程序,重复提示用户输入一个州的首府. ...

  5. Java //PP2.6 编写一个应用程序,将英里转换为千米(1英里等于1.60935千米)。以浮点数类型读取用户输入的英里数

    Java程序设计教程(第七版) John Lewis & William Loftus 电子工业出版社 PP2.6 编写一个应用程序,将英里转换为千米(1英里等于1.60935千米).以浮点数 ...

  6. 编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。

    编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接.整数相加和浮点数相加.要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃. pack ...

  7. c语言用户输入编程,C语言入门教程-Printf:读取用户输入

    Printf:读取用户输入 前面的程序很好,但要是如果能从用户那里读入值5和7而不是使用固定的值的话,那就更好了.可以改为下面的程序: #include int main() {int a, b, c ...

  8. linux获取read值,read命令读取用户输入

    read命令用于从终端或文件中读取用户输入,它读取整行输入,如果没有指定名称,读取的行被赋值给内部变量REPLY. read命令常用选项:-a,-p,-s,-t,-n 1.REPLY变量 $read ...

  9. Linux Shell 读取用户输入

    在Linux Shell下读取用户输入可以使用read命令. 用法: echo "Input something:" read input echo "Your inpu ...

最新文章

  1. js微信监听返回_JS监听微信、支付宝等移动app及浏览器的返回、后退、上一页按钮的事件方法...
  2. linux 物理内存用完了_调整linux内核尽量用内存,而不用swap
  3. node express创建项目步骤
  4. 泰山JDK8率先支持多种权重(笔画粗细)的字体绘制
  5. eXtremeComponents使用总结--1(转载)
  6. mysql 测试数据生成器_测试数据生成器
  7. 利用docker maven镜像打包springboot
  8. 华为机试:VLAN资源池
  9. Ansys 15.0 x64 安装
  10. 七十二烷基三硅醇poss|七十二烷基三硅醇笼形聚倍半硅氧烷
  11. 拼多多求变 200 天:撒钱百亿元,江湖人称拼爹爹?
  12. 用Rest assured作API自动化集成测试
  13. css判断iPhone样式
  14. 平台“运营+变现”万精油方法论:拉新→促活→留存→转化→裂变→提频
  15. Node.js轻量级Web框架Express4.x使用指南
  16. 80x86编程手册_编程从8到80
  17. SDUT-4100 C~K要找女朋友了!!!
  18. 【20211106】在技术上是如何实现分布式事务_V3(TCC)
  19. 分类模型计算混淆矩阵
  20. Android:相对布局RelativeLayout常用属性

热门文章

  1. A.CIMS 计算机集成管理系统,cims系统(smis系统是什么)
  2. layui导出html到pdf,layui打印html页面转成pdf
  3. PID控制器改进笔记之三:改进PID控制器之正反作用
  4. 使用IDA Pro动态调试SO文件
  5. [python]---从java到python(01)---基础与入门上手
  6. idea怎么更改推到github的路径_IDEA 拉取、上传、更新 项目到 Gitee+GitHub_超详细超简单版...
  7. Linux的实际操作:查询用户id切换用户su -
  8. xss植入_网站xss漏洞的利用过程
  9. php redis与me m,Redis(十) —— 为php增加redis扩展
  10. unity vscode没有智能提示_GAMES101课程环境搭建(Win10+VSCode)