问题
so I'm very very new to java and I'm having a slight problem. I'm adding various doubles and integers and trying to read data from a file, but when I try read data from the file after the second string, I am given an error. Anything before the second string is fine.
Here's my code;
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();
I am given this error when I try read anything after the second string;
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)
I am unsure at to why I am receiving this error, any help would be appreciated, thanks.
Here is the input file for those who were asking;
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
回答1:
Let's say you file looks like this
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
I just added an inFile.nextLine() after double Unit1Assistant4rate = inFile.nextDouble(); and it works fine for me. Before I added it, I got the same Exception you did. But now it's fine.
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");
Before I added the inFile.nextLine(). The exception pointed to this line double NumberofAssistantsUnit2 = inFile.nextDouble();. So I assumed the first part of code worked. It's just when the program tried to read the the second set of text, the String Unit2 = inFile.nextLine(); ate up the empty space after the nextDouble() as the String. So the next line would be Unit2 instead of the double you were expecting. This happens when combining the reading of Strings and other value types. I prefer to read line by line and split, to avoid this problem.
Edit after OP edit with file
Try this code out. It's Runnable. Works for me. Just replace the file name
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();
}
}
}
}
回答2:
The input file in the question has two non-numeric lines between the last number for Unit One and the first number for Unit Two. You need to do two nextLine() calls to skip both of those.
inFile.nextLine();
String Unit2 = inFile.nextLine();
来源:https://stackoverflow.com/questions/20311831/unable-to-read-double-or-int-after-first-adding-second-string