Java Scanner麻烦:如何跳到下一行?

时间:2011-11-25 02:22:37

标签: java java.util.scanner

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

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”仍然在Scanner的堆栈中(我猜它是堆栈)和选择查找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)***

2 个答案:

答案 0 :(得分:3)

你应该可以这样做:

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

由于此时您知道“结束”或“结束”仍然在Scanner中等待,您只需阅读下一行并且不执行任何操作。如果给出错误的输入,仍然存在关于更优雅地失败的问题,但这应该解决给定的问题。

答案 1 :(得分:-1)

你应该使用“\ r \ n”在使用java

编写文件时转到下一行