Я делаю текстовую программу, управляемую меню, которая использует класс сканера для приема как целых чисел, так и строк. Целые числа соответствуют параметрам меню, а строки используются для ввода данных пользователем.
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 во второй раз, это вызывает ошибку несоответствия типов, потому что "конец"/"КОНЕЦ" все еще находится в стеке сканера (я предполагаю, что это стек) и 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:
[email protected]
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
[email protected]
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)***