在Java中的try-catch方法中实现while循环

时间:2015-03-18 17:45:47

标签: java file-io while-loop

所以基本上我尝试使用我的代码的方法是查看payroll.txt文件,如下所示:

31718 PHILLIP LENNOX 55.0 20.00
11528 NANCY TROOPER 40.0 10.45
16783 JOHN CONNAUGHT 30.5 10.00
10538 PETER DUNCAN 45.0 10.75
21O15 JAMES HAROLD 32.0 10.50
61326 HARRY KUHN 25.0 12.30
82465 MICHELLE BENOIT 50.0 18.50
31816 DANIELLE RAYMOND 35.5 15.25
73745 JACK O'TOOLE 28.0 11.50

然后选择InputMismatchExceptions或低于10.35的双精度(如下面的代码所示)并在另一个名为payrollError.txt的文件中打印出整行。我到这里的问题基本上是打印到我的payrollError.txt文件:

31718 PHILLIP LENNOX 55.0 20.0
11528 NANCY TROOPER 40.0 10.45
10538 PETER DUNCAN 45.0 10.75
0 null null 0.0 0.0
0 null null 0.0 0.0
0 null null 0.0 0.0
0 null null 0.0 0.0
0 null null 0.0 0.0
(These lines go on infinitely)

我似乎无法弄清楚问题所在。我尝试了各种不同的方法,但根本没有打印出来,或者这个大量的无限打印输出。

  import java.io.*;
    import java.util.*;

public class Main {
public static void main(String[] args){

    List<Employee> ArrEmployee = new ArrayList<>(); //  array for employee objects

        try (PrintWriter txtOut = new PrintWriter("payrollError.txt")) {

            Scanner txtIn = new Scanner(new File("payroll.txt")); 

            while (txtIn.hasNext()) { // looping through the payroll.txt file and creating Employee objects from its data
                 long EmployeeNumber = 0;
                 String EmployeeName = null;
                 String LastName = null;
                 double HoursWorked = 0;
                 double HourlyWage = 0;

                 try{
                      EmployeeNumber = txtIn.nextLong();
                      EmployeeName = txtIn.next();
                      LastName = txtIn.next();
                      HoursWorked = txtIn.nextDouble();
                      HourlyWage = txtIn.nextDouble();

                        if (HourlyWage > 10.35){ 
                            throw new MinimumWageException(); // throws exception if the hourly wage is less than 10.35$
                                }

                        else
                            ArrEmployee.add(new Employee(EmployeeNumber,EmployeeName,LastName,HoursWorked,HourlyWage)); // creates Employee objects according to the input payroll.txt
                }
                catch (InputMismatchException n) { // catching long,strings and doubles in the payroll.txt that aren't valid
                     txtOut.println(EmployeeNumber + " " + EmployeeName + " " + LastName + " " + HoursWorked + " " + HourlyWage);
                     txtIn.hasNext();

                     }
                 catch (MinimumWageException z){
                     txtOut.println(EmployeeNumber + " " + EmployeeName + " " + LastName + " " + HoursWorked + " " + HourlyWage);
                     txtIn.hasNext();
                 }
                      }


        } catch (FileNotFoundException e) {
            System.out.println("File payroll.txt was not found.");

            }

    }

}

2 个答案:

答案 0 :(得分:1)

这里有一些问题。已经指出的是,你有(HourlyWage > 10.35)而不是(HourlyWage < 10.35)。主要问题是,在遇到输入不匹配后,由于在0中使用字母“O”而不是数字21O15,扫描程序会将令牌留在流中。这就是你进入无限循环的原因。你一遍又一遍地阅读不良输入。当然,没有像EmployeeName等变量被分配一个值,这就是为什么你看到从catch块打印0和null的原因。

要修复无限循环,您需要在返回循环顶部之前吃剩下的行。请注意txtIn.nextLine()而不是之前的txtIn.hasNext()。 (我不确定为什么你的catch块中有txtIn.hasNext()。)

catch (InputMismatchException n) { // catching long,strings and doubles in the payroll.txt that aren't valid
    txtOut.println("IME: " + EmployeeNumber + " " + EmployeeName + " " + LastName + " " + HoursWorked + " " + HourlyWage);
    txtIn.nextLine()
}

修改
要解决没有读入您尝试在catch块中打印的值的问题,您可以打印成功读取的值,然后打印nextLine()检索到的其余行:

            catch (InputMismatchException n) { // catching long,strings and doubles in the payroll.txt that aren't valid
                StringBuilder sb = new StringBuilder();
                sb.append(EmployeeNumber == 0 ? "" : (EmployeeNumber + " "));
                sb.append(EmployeeName == null ? "" : (EmployeeName + " "));
                sb.append(LastName == null ? "" : (LastName + " "));
                sb.append(HoursWorked == 0.0 ? "" : (HoursWorked + " "));
                sb.append(HourlyWage == 0.0 ? "" : (HourlyWage + " "));
                sb.append(txtIn.nextLine());
                txtOut.println(sb.toString());
            }

这并不完美,因为0.0可能是工作小时数的合法值,例如,但它可能足以满足您的目的。在任何情况下,一次只有一个问题:)

答案 1 :(得分:0)

您应该查看Scanner类的文档。它说

  

当扫描程序抛出InputMismatchException时,扫描程序不会   传递导致异常的令牌,以便可以检索它   或通过其他方法跳过。

这意味着,您应该拨打hasNext()

而不是在catch块中调用skip()