在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
答案 0 :(得分:1)
您应该使用Scanner类中的hasNextXXXX()方法来确保有一个可以读取的整数。
问题是你被称为nextInt(),它从Scanner对象所指向的流中读取下一个整数,如果没有要读取的整数(即如果输入用尽,那么你将看到NoSuchElementException)< / p>
在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();
答案 1 :(得分:0)
扫描程序抛出:NoSuchElementException - 如果输入已用完。
您确定输入有更多元素吗?
我会将代码重构为:
if(input!= null&amp;&amp; input.hasNext()){ .. .. }
和
if(input!= null&amp;&amp; input.hasNextLine()){ .. .. }
答案 2 :(得分:0)
我认为它与您的扫描仪有关。
扫描仪输入法使用缓冲区来捕获和存储输入的数据。
Nextline将捕获所有数据到返回字符的末尾,这会清空缓冲区。
所有其他扫描程序方法(例如Nextint)从缓冲区捕获数据直到返回字符,由于剩余的返回字符,它基本上会过早地结束您的下一次扫描程序捕获。
尝试添加input.nextline();从任何扫描仪方法捕获输入后清除缓冲区并防止扫描仪输入方法出现问题。
这就是我理解的方式,如果有人有更好的解释,我会全力以赴。
答案 3 :(得分:0)
我猜你正在运行应用程序的方式没有将它绑定到控制台,因此System.in
只是在阅读EOF。例如,您正在通过(dumber)IDE运行它,或者您正在使用旧版本的Maven来执行它。尝试直接从控制台(命令提示符)运行,看看它是否有效。
如果这是一个IDE问题,请告诉我们它是哪一个,这样可能会受到羞辱。