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

Implementing a while loop inside a try-catch method in Java

所以基本上我尝试用我的代码做的是查看一个 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.");

            }

    }

}

你这里有一些问题。已经指出的是您有 (HourlyWage > 10.35) 而不是 (HourlyWage < 10.35)。主要问题是,在您遇到因在 21O15 中使用字母 "O" 而不是数字 0 而导致的输入不匹配后,扫描器将令牌留在流中。这就是你进入无限循环的原因。你一遍又一遍地阅读错误的输入。当然 none 的变量,如 EmployeeName 等被分配了一个值,这就是为什么你看到从 catch 块打印 0 和空值的原因。

要修复无限循环,您需要在返回循环顶部之前吃掉该行的剩余部分。请注意 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():[=21 检索到的行的其余部分=]

            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 可能是工作时间的合法值,例如,但也许它足以满足您的目的。无论如何,一次一个问题:)

您应该查看扫描程序 class 的文档。它说

When a scanner throws an InputMismatchException, the scanner will not pass the token that caused the exception, so that it may be retrieved or skipped via some other method.

这意味着,您应该调用 skip()

,而不是在 catch 块中调用 hasNext()