“线程中的异常”;“主要”;java.util.NoSuchElementException,大量错误
对于Java,我是个新手,我对我正在设计的这个新程序有一个问题。我相信这可能与扫描仪有关,但我不太确定。很抱歉,如果我的格式设置被关闭,这是我的第一篇文章。任何帮助都将不胜感激“线程中的异常”;“主要”;java.util.NoSuchElementException,大量错误,java,java.util.scanner,Java,Java.util.scanner,对于Java,我是个新手,我对我正在设计的这个新程序有一个问题。我相信这可能与扫描仪有关,但我不太确定。很抱歉,如果我的格式设置被关闭,这是我的第一篇文章。任何帮助都将不胜感激 package commissioncalculationprogram; import java.util.Scanner; public class calculate { public static void main(String args[]) { Scanner input = n
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]));
}
}
}
使用提供的建议
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
input.nextInt()或input.nextDouble()等。。。
扫描仪抛出:NoTouchElementException-如果输入用尽
您确定input包含更多元素吗
我将重构代码,如下所示:
if(input!=null&&input.hasNext()){
..
..
}
及
if(input!=null&&input.hasNextLine()){
..
..
}我相信这和你的扫描仪有关 扫描仪输入法使用缓冲区捕获和存储输入的数据 Nextline将捕获返回字符末尾的所有数据,从而清空缓冲区 所有其他扫描仪方法(如Nextint)都从缓冲区捕获数据,直到返回字符之前,这基本上会由于剩余的返回字符而提前结束下一次扫描仪捕获 尝试添加输入。nextline();从任何扫描仪捕获输入后,请使用以下方法清除缓冲区并防止扫描仪输入方法出现问题
这就是我的理解,如果有人有更好的解释,我会洗耳恭听。您应该使用Scanner类中的hasNextXXXX()方法来确保有一个整数可以读取 问题是您被称为nextInt(),它从扫描仪对象指向的流中读取下一个整数,如果没有整数可读取(即,如果输入已耗尽,则您将看到NoTouchElementException) 在JavaDocs中,nextInt()方法将在以下条件下引发这些异常: InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围 NoTouchElementException-如果输入用尽 IllegalStateException-如果此扫描仪已关闭 您可以使用hasNextInt()方法轻松解决此问题:
我猜您运行应用程序的方式不会将其绑定到控制台,因此
System.in
只是读取EOF。例如,您正在通过(dumber)IDE运行它,或者您正在使用旧版本的Maven来执行它。尝试直接从控制台(您的命令提示符)运行,看看它是否有效
如果这是一个IDE问题,请让我们知道它是哪一个,这样它会感到羞耻。您能告诉我们
calculate.java
文件第14行的代码是什么吗?请注意,这不是编译错误,而是运行时的异常我想您正在关闭代码中的某个扫描程序,然后,您正试图从已关闭的stream@Andrewint SIZE=input.nextInt();这是我在14号排的。我同意。初始化外部大小。如果不是常量,则使用小写。这是System.in
。我想,如果没有输入,它应该等待。事实上,Javadoc中的第一个示例就是这样做的。在系统中应该没有必要。在@Mayank中,我尝试了使用您建议的方法,但收到了更多的错误。你会把它放在我开发的代码中的什么地方?@Dwill34我已经编辑了代码。你需要把这篇文章放在你试图阅读的地方。如果仍有错误,请发布代码和错误。code
commcalculator/commcalculator.java:30:error:找不到符号输入。nextLine();^符号:变量输入位置:类commcalculator commcalculator/commcalculator.java:32:错误:找不到符号名称[i]=input.nextLine();^symbol:变量输入位置:类commcalculator commcalculator/commcalculator.java:34:错误:找不到symbol sale[i]=input.nextDouble();^符号:变量输入位置:class commcalculator 3 errorscode
@MayankAgarwal很抱歉格式化,但这就是我从更改中得到的结果。我使用的是NetBeans 8.0,我可以从那里很好地运行程序;但是,我遇到异常错误。@Dwill34:请尝试直接从命令提示符/终端运行。不是从Netbeans内部。@Dwill34:另外,当您运行java-version
时,您运行的是什么操作系统,以及从控制台运行的操作系统?它说什么?@Dwill34:您还有一些不相关的样式问题,这些问题非常严重,您将因此失去分数<代码>大小
不是常数,因此不应大写<代码>销售人员
似乎充当了一个实用类,而不是保存每个销售人员的数据,这很可能是他们的初衷。
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 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();