我不确定为什么我收到这个错误,到目前为止我有一个网站的成员对我有所帮助,但我们都不确定。任何帮助将不胜感激,谢谢。
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;");
}
}
如果需要,输入文件;
Unit One
4
32 8
38 6
38 6
16 7
Unit Two
0
Unit Three
2
36 7
36 7
Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5
Unit Five
4
32 6.5
32 8
32 7
32 8
Unit Six
5
38 7
30 6.5
24 8
24 8
24 8
Unit Seven
0
Unit Eight
1
40 12
Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7
给出错误;
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("Comparisons are as follows;");
根本不会打印出来。
答案 0 :(得分:0)
您正在检查
input.hasNextLine()
告诉您至少一个更多行。 然后你读取该行,而不检查是否有其他数据可用,你可以调用
input.next();
并获得例外。这意味着只有一行,然后是文件的结尾:)