线程中的Uknown异常;“主要”;java.util.NoSuchElementException
我不确定为什么我会收到这个错误,到目前为止,我已经有一个网站的成员好心地帮助我,但我们都不确定。任何帮助都将不胜感激,谢谢线程中的Uknown异常;“主要”;java.util.NoSuchElementException,java,arrays,while-loop,double,Java,Arrays,While Loop,Double,我不确定为什么我会收到这个错误,到目前为止,我已经有一个网站的成员好心地帮助我,但我们都不确定。任何帮助都将不胜感激,谢谢 import java.io.FileNotFoundException; import java.io.FileReader; import java.io.InputStreamReader; import java.util.Scanner; public class Assignment3 { static Scanner console = new Scann
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Assignment3 {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileReader("AssistantHoursAndRates.txt"));
double UnitRM1;
System.out.println("Enter recommended maximum staff cost of Unit 1");
UnitRM1 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit1 = "+UnitRM1);
System.out.printf("%10s\n", " ");
double UnitRM2;
System.out.println("Enter recommended maximum staff cost of Unit 2");
UnitRM2 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit2 = "+UnitRM2);
System.out.printf("%10s\n", " ");
double UnitRM3;
System.out.println("Enter recommended maximum staff cost of Unit 3");
UnitRM3 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit3 = "+UnitRM3);
System.out.printf("%10s\n", " ");
double UnitRM4;
System.out.println("Enter recommended maximum staff cost of Unit 4");
UnitRM4 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit4 = "+UnitRM4);
System.out.printf("%10s\n", " ");
double UnitRM5;
System.out.println("Enter recommended maximum staff cost of Unit 5");
UnitRM5 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit5 = "+UnitRM5);
System.out.printf("%10s\n", " ");
double UnitRM6;
System.out.println("Enter recommended maximum staff cost of Unit 6");
UnitRM6 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit6 = "+UnitRM6);
System.out.printf("%10s\n", " ");
double UnitRM7;
System.out.println("Enter recommended maximum staff cost of Unit 7");
UnitRM7 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit7 = "+UnitRM7);
System.out.printf("%10s\n", " ");
double UnitRM8;
System.out.println("Enter recommended maximum staff cost of Unit 8");
UnitRM8 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit8 = "+UnitRM8);
System.out.printf("%10s\n", " ");
double UnitRM9;
System.out.println("Enter recommended maximum staff cost of Unit 9");
UnitRM9 = console.nextDouble ();
System.out.println("Recommended maximum staff cost of Unit9 = "+UnitRM9);
double[] totals = new double[9];
int unit = 1;
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);
double total = 0;
int assistants = input.nextInt();
System.out.println("Number of Assistants " + assistants);
System.out.println("Hours Rate");
System.out.println("------------");
for (int i = 0; i < assistants; i++) {
int hours = input.nextInt();
System.out.print(hours + " ");
double rate = input.nextDouble();
System.out.println(rate);
total += (hours * rate);
}
System.out.println("Total cost of Unit " + unit + " is " + total);
System.out.println();
totals[unit - 1] = total;
unit++;
if (input.hasNextLine()) {
input.nextLine();
input.next();
}
}
System.out.println("Comparisons are as follows;");
}
}
给出的错误
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Assignment3.main(Assignment3.java:108)
错误指向第108行
input.next();
最终打印输出System.out.println(“比较如下;”代码>根本不打印。您正在与
input.hasNextLine()
这说明至少还有一行。
然后你读那一行,不检查是否有额外的数据可供调用
input.next();
然后得到一个例外。这意味着只有一行,然后是文件的结尾:)
input.next();