java - 线程 "main"java.util.NoSuchElementException 中的异常,大量错误

标签 java java.util.scanner

我对 Java 很陌生,我对我正在设计的这个新程序有疑问。我相信这可能与扫描仪有关,但我不太确定。如果我的格式不正确,我深表歉意,这是我的第一篇文章。任何帮助将不胜感激。

package commissioncalculationprogram;

import java.util.Scanner;

public class calculate {
    public static void main(String args[]) {
        Scanner input = new Scanner(System.in); // for user input

        //create new object to compute compensation and  find the highest earner
        Salesman a = new Salesman ();

        System.out.print("How many salespersons do you want to compare: ");
        int SIZE = input.nextInt(); //**** Line 14 in original code ****

        String[] name = new String[SIZE];
        double[] sale = new double[SIZE];
        double[] compensation = new double[SIZE];

        // input data for each person and compute compensation for that one
        for (int i = 0; i < SIZE; i++) {
            input.nextLine();
            System.out.print("Please enter name of person " + (i+1) + ": ");
            name[i] = input.nextLine();           
            System.out.print("Please enter total annual sales of person " + (i+1) + ": $");
            sale[i] = input.nextDouble();
            compensation[i] = a.calculation(sale[i]);
        }

        // find index of the highest earner in array
        int highest = a.highest_earner(compensation, SIZE);

        // display the highest earner
        System.out.println("\nThe highest earner is: \n" + name[highest] + 
                       " with total sales: $" + sale[highest] +
                       " and total compensation: $" + compensation[highest]);

        // calculate and display the additional amount of sales that each salesperson 
        // must achieve to match or exceed the higher of the two earners
        System.out.println("\nAdditional amount of sales needed for others to match the higgest earners:");
        System.out.format("%-25s%10s\n", "   Name", "Sale amount needed");

        for (int i = 0; i < SIZE; i++) {
            if (i == highest) {
                continue;
            } // skip the highest earner

            //System.out.println(name[i] + "\t\t$" + (sale[highest] - sale[i]));
            System.out.format("%-25s%10s\n", name[i], (sale[highest] - sale[i]));
        }
    }
}

当我尝试编译代码时,出现了这些错误。

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 commissioncalculationprogram.calculate.main(calculate.java:14)

使用提供的建议。

    Scanner s = new Scanner(System.in); // for user input
    //create new object to compute compensation and  find the highest earner
    salescalc a = new salescalc ();
    // ask user enter number of salespersons
    int size = 0;
    System.out.print("How many salespersons do you want to compare: ");
    if(s.hasNextInt())
    {
    size = s.nextInt();
    }
    s.close();

     New errors

     commcalculator/commcalculator.java:30: error: cannot find symbol
        input.nextLine();
        ^
      symbol:   variable input
    location: class commcalculator
    commcalculator/commcalculator.java:32: error: cannot find symbol
        name[i] = input.nextLine();           
                  ^
    symbol:   variable input
    location: class commcalculator
    commcalculator/commcalculator.java:34: error: cannot find symbol
        sale[i] = input.nextDouble();
                  ^
    symbol:   variable input
    location: class commcalculator
    3 errors

最佳答案

您应该使用 Scanner 类中的 hasNextXXXX() 方法来确保有一个整数可供读取。

问题是你被称为 nextInt() ,它从 Scanner 对象指向的流中读取下一个整数,如果那里没有整数可读取(即,如果输入耗尽,那么你将看到 NoSuchElementException)

根据 JavaDocs,nextInt() 方法将在这些条件下抛出这些异常:

InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围 NoSuchElementException - 如果输入已用完 IllegalStateException - 如果此扫描器已关闭 您可以使用 hasNextInt() 方法轻松解决此问题:

    Scanner s = new Scanner(System.in);
    int size= 0;

 System.out.print("How many salespersons do you want to compare: ");
    if(s.hasNextInt()) 
    {
       size= s.nextInt();
    }

    s.close();

关于java - 线程 "main"java.util.NoSuchElementException 中的异常,大量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24249917/

相关文章:

java - karaf- assembly 4.0.5 - 成功的 Maven 构建结束时不会生成 zip ard tar.gz 文件

java - 使用扫描仪处理用户输入

java - Scanner.nextInt(),超出范围避免?

Java读取文件类不创建数组列表

java - 在控制台中输入多个数字

java泛型参数化类型

java - 关于字符串比较的问题

java - 如何在 Java 中生成非安全 key 对?

java - Java 客户端/服务器应用程序的负载测试

java - 从文件读取时输入不匹配