扫描仪出现混淆错误

时间:2013-06-05 15:26:22

标签: java java.util.scanner

我一直收到这个错误,我不知道为什么:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Daily.takingData(Daily.java:33)
at Daily.main(Daily.java:20)

这是我的代码:

import java.io.*;
import java.util.Scanner;

public class Daily    
{

    private static int size;

    public static void main(String[] args) throws Exception {
        System.out.println("Please enter amount of rows");
        Scanner scan1 = new Scanner(System.in);
        size = scan1.nextInt();
        scan1.close();

        System.out.println();
        takingData(size);
    }

    public static void takingData(int rows) throws Exception {
        System.out.println("Enter 1 To View Number of Markets");
        System.out.println("Enter 2 To View Start and End Dates of Markets");
        System.out.println("Enter 3 To View Start and End Dates of Contracts");
        System.out.println("Enter 4 To View Averages of Markets");
        System.out.println("Enter 0 To Quit Program");
        int choice = 0;
        Scanner scan2 = new Scanner(System.in);
        choice = scan2.nextInt();
        System.out.println("Got here");
        scan2.close();
        if (choice == 0)
            System.exit(0);
    }
} 

我的出局是:

Enter 1 To View Number of Markets
Enter 2 To View Start and End Dates of Markets
Enter 3 To View Start and End Dates of Contracts
Enter 4 To View Averages of Markets
Enter 0 To Quit Program
(error here)

4 个答案:

答案 0 :(得分:1)

问题是你正在关闭Scanner的第一个实例

scan1.close();

正在关闭关联的InputStreamSystem.in) - 这会阻止第二个Scanner实例从流中读取。

请勿关闭扫描仪。您还可以创建一个Scanner实例来读取所有值。

从设计的角度来看,我将从static方法转移到OO方法,并在Scanner的构造函数中创建单个Daily实例,并且所有方法都成为实例方法。这有助于Object的可测试性。

public class Daily {

    private final Scanner scanner;

    public Daily() {
        scanner = new Scanner(System.in);
    }

    public int getRows() {

        System.out.println("Please enter amount of rows");
        return scanner.nextInt();
    }

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

        Daily daily = new Daily();
        int rows = daily.getRows();

        int mainOption = daily.getMainOption(rows);
        switch (mainOption) {
        case 0: // TODO: refactor to use enums
            System.exit(0);
        }
    }

    public int getMainOption(int rows) {
        System.out.println("Enter 1 To View Number of Markets");
        System.out.println("Enter 2 To View Start and End Dates of Markets");
        System.out.println("Enter 3 To View Start and End Dates of Contracts");
        System.out.println("Enter 4 To View Averages of Markets");
        System.out.println("Enter 0 To Quit Program");

        return scanner.nextInt();
    }
}

答案 1 :(得分:1)

您收到错误是因为您在扫描后立即关闭扫描仪:

size = scan1.nextInt();
scan1.close();

然后尝试再次在takingData

中扫描

删除scan1.close();以外的takingData

当您关闭Scanner时,它正在扫描的InputStream也会关闭,在这种情况下,您的System.in正在关闭。

  

当扫描仪关闭时,如果源实现了Closeable接口,它将关闭其输入源。

取自Scanner javadocs

答案 2 :(得分:0)

您的问题的答案如下:https://stackoverflow.com/a/13042296/1688441提问: java.util.NoSuchElementException - Scanner reading user input

我引用:

  

当你在第一种方法中调用sc.close()时,它不仅会关闭你的   扫描程序,但也关闭您的System.in输入流。你可以   通过在第二种方法的最顶部打印其状态来验证:

System.out.println(System.in.available()); 
     

所以现在当你重新实例化第二种方法中的Scanner时,它找不到任何打开方式   System.in流,因此异常。

答案 3 :(得分:-1)

摆脱初始的int选择,并试试这个:

int choice = scan2.nextInt();

不应该有所作为,但它可能有所帮助。