JAVA循环防崩溃,Java – For循环无法完成,仅在三个循环后崩溃

这是一些课程作业的一部分,我宁愿为自己弄清楚,所以如果可能的话,请你不要直接给出答案,但要指出我正确的方向或告诉我我的错误在哪里.

我必须创建一些代码来读取文件中的文本,然后用它做一些其他的事情,我的问题是在读取第三个文本块后,我的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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值