所以我对java非常新,我遇到了一些问题。我正在添加各种双精度和整数并尝试从文件中读取数据,但是当我尝试在第二个字符串后从文件中读取数据时,我收到了一个错误。第二个字符串之前的任何东西都没问题。
这是我的代码;
import java.util.*;
import java.io.*;
public class Assignment3 {
static Scanner console = new Scanner (System.in);
public static void main(String[] args) throws FileNotFoundException {
Scanner inFile = new Scanner(new FileReader("AssistantHoursAndRates.txt"));
String Unit1 = inFile.nextLine();
double NumberofAssistantsUnit1 = inFile.nextDouble();
double Unit1Assistant1hours = inFile.nextDouble();
double Unit1Assistant1rate = inFile.nextDouble();
double Unit1Assistant2hours = inFile.nextDouble();
double Unit1Assistant2rate = inFile.nextDouble();
double Unit1Assistant3hours = inFile.nextDouble();
double Unit1Assistant3rate = inFile.nextDouble();
double Unit1Assistant4hours = inFile.nextDouble();
double Unit1Assistant4rate = inFile.nextDouble();
String Unit2 = inFile.nextLine();
double NumberofAssistantsUnit2 = inFile.nextDouble();
当我在第二个字符串后尝试读取任何内容时,我收到此错误;
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at Assignment3.main(Assignment3.java:28)
我不确定为什么我收到此错误,任何帮助都将不胜感激,谢谢。
以下是那些要求的人的输入文件;
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
答案 0 :(得分:0)
假设您的文件看起来像这样
Unit1
10.00
40.00
15.00
40.00
15.00
40.00
15.00
40.00
15.00
Unit1
10.00
40.00
15.00
40.00
15.00
40.00
15.00
40.00
15.00
我刚刚在inFile.nextLine()
之后添加了double Unit1Assistant4rate = inFile.nextDouble();
,它对我来说很好。在我添加它之前,我得到了与你相同的异常。但是现在没关系。
String Unit1 = inFile.nextLine();
double NumberofAssistantsUnit1 = inFile.nextDouble();
double Unit1Assistant1hours = inFile.nextDouble();
double Unit1Assistant1rate = inFile.nextDouble();
double Unit1Assistant2hours = inFile.nextDouble();
double Unit1Assistant2rate = inFile.nextDouble();
double Unit1Assistant3hours = inFile.nextDouble();
double Unit1Assistant3rate = inFile.nextDouble();
double Unit1Assistant4hours = inFile.nextDouble();
double Unit1Assistant4rate = inFile.nextDouble();
inFile.nextLine();
String Unit2 = inFile.nextLine();
double NumberofAssistantsUnit2 = inFile.nextDouble();
double Unit2Assistant1hours = inFile.nextDouble();
double Unit2Assistant1rate = inFile.nextDouble();
double Unit2Assistant2hours = inFile.nextDouble();
double Unit2Assistant2rate = inFile.nextDouble();
double Unit2Assistant3hours = inFile.nextDouble();
double Unit2Assistant3rate = inFile.nextDouble();
double Unit2Assistant4hours = inFile.nextDouble();
double Unit2Assistant4rate = inFile.nextDouble();
System.out.println("This works fine");
在我添加inFile.nextLine()
之前。异常指向此行double NumberofAssistantsUnit2 = inFile.nextDouble();
。所以我假设代码的第一部分工作。只是当程序试图读取第二组文本时,String Unit2 = inFile.nextLine();
在nextDouble()
之后将空格作为字符串。因此下一行将是Unit2
,而不是您期望的double
。将String
s和其他值类型的读数组合在一起时会发生这种情况。我更喜欢逐行阅读和分割,以避免这个问题。
使用文件进行OP编辑后编辑
尝试使用此代码。它是Runnable。适合我。只需替换文件名
即可import java.io.FileNotFoundException;
import java.io.FileReader;
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("aptunits.txt"));
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();
unit++;
if (input.hasNextLine()) {
input.nextLine();
input.next();
}
}
}
}
答案 1 :(得分:0)
问题中的输入文件在第一单元的最后一个数字和第二单元的第一个数字之间有两条非数字线。你需要做两个nextLine()调用来跳过这两个。
inFile.nextLine();
String Unit2 = inFile.nextLine();