我的循环中出错了。线程“main”java.util.NoSuchElementException中的异常。错误是我的扫描仪?

时间:2013-03-22 15:38:32

标签: java arrays java.util.scanner

好吧,看起来我的主要课遇到了一些问题。它在第二次运行时打破了我的循环并显示和错误。它说我的扫描仪从菜单中读取用户选择是否产生错误?怎么样,它在第一个循环中运行,但由于某种原因它不能再运行。

"action = new Scanner(System.in).nextInt();" 

正在生成错误。有谁知道为什么会发生这种情况,因为当用户选择菜单选项时,读取用户输入的整数非常重要。

import java.util.Scanner; 
import java.io.FileWriter; 
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.String; 
import java.lang.System;  

public class MainActions {

    public static void main(String args[]) throws IOException {

        int action = 0;

        while (action != 6) {

            Contact.menu();

            new Scanner(System.in);

            action = new Scanner(System.in).nextInt();

            if (action <= 0 || action > 6) {
                System.out.println("Invalid selection. ");
            }

            switch (action) {
            case 1:
                MainActions.addContactInfo();
                break;

            case 2:
                break;

            case 3:
                break;

            case 4:
                break;

            case 5:
                break;
            }
        }
    }

    public static void addContactInfo() {

        Contact contact;
        contact = new Contact();

        Scanner reader = new Scanner(System.in);
        System.out.println("Enter Contact Last Name:");
        String lastname = reader.nextLine();
        contact.setLastName(lastname);
        System.out.println("Enter Contact First Name: ");
        contact.setFirstName(reader.nextLine());
        System.out.println("Enter Contact Street Address: ");
        contact.setHouseAddress(reader.nextLine());
        System.out.println("Enter Contact City: ");
        contact.setCity(reader.nextLine());
        System.out.println("Enter Contact Zip Code: ");
        contact.setZip(reader.nextLine());
        System.out.println("Enter Contact Email: ");
        contact.setEmail(reader.nextLine());
        System.out.println("Enter Contact Phone Number: ");
        contact.setPhone(reader.nextLine());
        System.out.println("Enter Contact Notes: ");
        contact.setNotes(reader.nextLine());

        if (lastname.trim().equals("")) {
            System.out.println("\nLast name was blank, contact not saved.");
            System.exit(0);
        } else {
            ContactList list;
            list = new ContactList();
            list.add(contact);
            list.save();
            Contact c = contact;
            try (PrintWriter output = new PrintWriter(new FileWriter("contactlist.csv", true))) {
                output.printf("%s\r\n", c);
            } catch (Exception e) {}
        }
        reader.close();
    }
}

控制台:

1. Enter a new person
2. Print the contact list
3. Retrieve a person's information by last name
4. Retrieve a person's information by email address
5. Retrieve all people who live in a given zip code
6. Exit
1
Enter Contact Last Name:
asdf
Enter Contact First Name: 
asdf
Enter Contact Street Address: 
asdf
Enter Contact City: 
asdf
Enter Contact Zip Code: 
asdf
Enter Contact Email: 
asdf
Enter Contact Phone Number: 
asdf
Enter Contact Notes: 
asdf

Contact information has been saved.

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at MainActions.main(MainActions.java:33)
1. Enter a new person
2. Print the contact list
3. Retrieve a person's information by last name
4. Retrieve a person's information by email address
5. Retrieve all people who live in a given zip code
6. Exit

2 个答案:

答案 0 :(得分:2)

替换

new Scanner(System.in);

action = new Scanner(System.in).nextInt();

使用

Scanner scan = new Scanner(System.in);

action = scan.nextInt();

答案 1 :(得分:1)

new Scanner(System.in); // Remove this. Not needed.
action = new Scanner(System.in).nextInt();

等等,这不是问题。这是实际问题。

reader.close();

addContactInfo()中的这一行是罪魁祸首。删除它,您的代码将起作用。

reader.close()会关闭您方法中的Scanner(扫描System.in),因为两者都在扫描System.in

希望这能解决您的问题。我碰巧找到了this question on SO。请查看此内容以获取更详细的说明。

文档说: -

public void close()抛出IOException

  

关闭此输入流并释放所有关联的系统资源   用这个流。普遍的合约是它关闭了   输入流。封闭流不能执行输入操作   不能重新开放。