gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:17:41 25 4
gpt4 key购买 nike

我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com