gpt4 book ai didi

Java 扫描器故障 : how to skip to the next line?

转载 作者:行者123 更新时间:2023-11-30 04:56:51 24 4
gpt4 key购买 nike

我正在制作一个基于文本的菜单驱动程序,它使用扫描仪类来接收整数和字符串。整数对应于菜单选项,而字符串用于接受用户输入。

private static Scanner userInput = new Scanner(System.in);

public static void main(String[] args)
{
//Will be used to initiate the while-loop
int start = 1;

while(start == 1)
{
System.out.print(Messages.printMenu());
**int choice = new Integer(userInput.nextLine());**

switch(choice)
{
case 1:

System.out.println(Messages.askForAuthor());
String author = userInput.nextLine();

System.out.println(Messages.askForRecepName());
String recepName = userInput.nextLine();

System.out.println(Messages.askForEmailAdd());
String recepEmail = userInput.nextLine();

System.out.println(Messages.askForSubject());
String subject = userInput.nextLine();

System.out.println(Messages.askForTextBody());
String textBody = "";
***while(!userInput.hasNext("end") && !userInput.hasNext("END"))***
{
textBody += userInput.nextLine() + "\n";
}

System.out.println(author);
System.out.println(recepName);
System.out.println(recepEmail);
System.out.println(subject);
System.out.println(textBody);
break;

“**”包围的部分是出现问题的地方。该程序在第一次运行期间运行得很好,但是当它第二次再次进入 while 循环时,它会导致类型不匹配错误,因为“end”/“END”仍在扫描器的堆栈中(我猜它是堆栈)和 choice 查找 int。

这是输出:

Document Storage System Menu
============================
1 - Create and store an e-mail
2 - Create and store a memo
3 - Create and store a report
4 - Display a document
5 - List all active documents
6 - List all archived documents
7 - Locate documents containing a specific word or phrase
8 - Archive a document
9 - Retrieve a document from the archive
10 - Clear the archive
99 - Quit

Enter your choice: 1
Please enter author:
Agent Smith
Please enter the recipient's name:
Neo
Please enter the recipient's e-mail address:
Neo@zion.net
Please enter the subject:
Notification for Eviction!
Please enter Enter text body (type END on separate line to stop):
All your base are belong to us
end
Agent Smith
Neo
Neo@zion.net
Notification for Eviction!
All your base are belong to us

Document Storage System Menu
============================
1 - Create and store an e-mail
2 - Create and store a memo
3 - Create and store a report
4 - Display a document
5 - List all active documents
6 - List all archived documents
7 - Locate documents containing a specific word or phrase
8 - Archive a document
9 - Retrieve a document from the archive
10 - Clear the archive
99 - Quit

Enter your choice: Exception in thread "main" java.lang.NumberFormatException: For input string: "end"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:449)
at java.lang.Integer.<init>(Integer.java:660)
at proj4.Project4.main(Project4.java:20)***

最佳答案

你应该能够做到这一点:

while(!userInput.hasNext("end") && !userInput.hasNext("END"))
{
textBody += userInput.nextLine() + "\n";
}
userInput.nextLine();

由于此时您知道“end”或“END”仍在扫描仪中等待,因此您可以只读取下一行而不对其执行任何操作。如果给出错误的输入,仍然存在更优雅地失败的问题,但这应该可以解决给定的问题。

关于Java 扫描器故障 : how to skip to the next line?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8264258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com