0

我不断收到此错误,我不知道为什么:

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

4 回答 4

1

问题是你要关闭Scanner这里的第一个实例

scan1.close();

正在关闭关联的InputStream( System.in) - 这可以防止Scanner从流中读取的第二个实例。

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

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

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();
    }
}
于 2013-06-05T15:30:37.083 回答
1

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

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

然后尝试再次扫描takingData

删除scan1.close();您的takingData.

当你关闭 aScanner时,它正在扫描的InputStream那个也被关闭,在这种情况下你的 System.in 正在被关闭。

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

取自Scanner javadocs

于 2013-06-05T15:38:42.217 回答
0

您的问题的答案在这里:https ://stackoverflow.com/a/13042296/1688441问题: java.util.NoSuchElementException - 扫描仪读取用户输入

我引用:

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

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

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

于 2013-06-05T15:41:51.757 回答
-1

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

int choice = scan2.nextInt();

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

于 2013-06-05T15:32:42.123 回答