这是一些课程作业的一部分,我宁愿为自己弄清楚,所以如果可能的话,请你不要直接给出答案,但要指出我正确的方向或告诉我我的错误在哪里.
我必须创建一些代码来读取文件中的文本,然后用它做一些其他的事情,我的问题是在读取第三个文本块后,我的for循环失败了.
这是我必须阅读的文字
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
…
这到第9单元
它是与我的代码在同一目录中的.txt文件.
这是我的代码.
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.util.*
;
public class stringvariable {
public static void main(String[] args) throws FileNotFoundException
{
String shop; //shop unit
int num; // number of sales assistants
int num2 = 9; // number of units
int hour; //hours worked
int rate; // rate of pay
int total = 0; // total cost
int i = 0;
int sum;
Scanner inFile = new Scanner (new FileReader ("4001Comp-CW1-TASK3-Infile.txt")); //opens the CW1-task3-Infile file
for (int b = 0; b
{
shop = inFile.nextLine();
num = inFile.nextInt();
for (i = 0; i
{hour = inFile.nextInt();
rate = inFile.nextInt();
total += (rate*hour);
System.out.println(total);}
System.out.println(shop +"s total cost is "+ total);
shop = inFile.nextLine();
shop = inFile.nextLine();
num = 0;
hour= 0;
rate = 0;
total = 0; }
}
}
我的打印输出正是我需要的,直到’unit 4′
256
484
712
824
Unit Ones total cost is 824
Unit Twos total cost is 0
252
504
Unit Threes total cost is 504
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at stringvariable.main(stringvariable.java:28)