从文件扫描时出现 InputMismatchException

InputMismatchException when scanning from a file

我正在编写一个程序,从输入文件中获取信息(名字和姓氏、小时费率和工作时间)计算净工资并将其写入不同的文件。文件 wages.txt 具有以下内容:

First Name, Last Name, Hourly pay, Hours Worked
Bruce, Wayne, 7.25, 40
John, Jones, 8.50, 45
Clark, Kent, 7.25, 42

输出应该是:

Last Name, First Name, Total Pay
Wayne, Bruce, 290
Jones, John, 361.25
Kent, Clark, 297.25

加班费是时薪的一半。费率是双倍的,工作时间是整数。在我的测试中,输出格式行被写入文件,当程序收集计算工资的信息时抛出异常。

static void wages(){
    //input file
    String file_name = "wages.txt";
    File input_file = new File(file_name);
    Scanner in_file = null;

    //This is the format line that will be wrote to the output file
    String out_format_line = "Last Name, First Name, Total Amount";

    //output file
    String file2_name = "out.txt";
    File output_file = new File(file2_name);
    PrintWriter out_file = null;

    try{
        out_file = new PrintWriter(output_file);
    }
    catch(FileNotFoundException ex){
        System.out.println("Error: The output file does not exist");
        ex.printStackTrace();
        System.exit(0);
    }
    try{
        in_file = new Scanner(input_file);
        in_file.useDelimiter("[,\n]\\s");
    }
    catch(FileNotFoundException ex){
        System.out.println("Error: The input file does not exist");
        ex.printStackTrace();
        System.exit(0);
    }
    try{
        //This line is useless and is removed with this variable
        String in_format_line = in_file.nextLine();
        //writes format line for output file
        out_file.print(out_format_line);
        while(in_file.hasNextLine()){
            //gathers relavent materials for calulating pay
            String first_name = in_file.next();
            String last_name = in_file.next();
            double rate = in_file.nextDouble();
            double over_rate = rate/2;
            int hours = in_file.nextInt();

            //calculates net pay with overtime
            if(hours > 40){
                int overtime = hours - 40;

                double norm_pay = 40*rate;
                double over_pay = overtime * over_rate;

                double net_pay = norm_pay + over_pay;

                out_file.print(last_name);
                out_file.print(", ");
                out_file.print(first_name);
                out_file.print(", ");
                out_file.println(net_pay);
            }
            //calculates net pay without overtime
            else{
                double net_pay = hours*rate;

                out_file.print(last_name);
                out_file.print(", ");
                out_file.print(first_name);
                out_file.print(", ");
                out_file.println(net_pay);
            }
        }
    }
    catch(InputMismatchException ex){
        System.out.println("Error: File format is incorrect");
        ex.printStackTrace();
    }
    finally{
        in_file.close();
        out_file.close();
    }
}

}

当 ex.printStackTrace() 为 运行 时,控制台显示:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at guided.exercise.pkg4.GuidedExercise4.wages(GuidedExercise4.java:120)
at guided.exercise.pkg4.GuidedExercise4.main(GuidedExercise4.java:18)

问题出在分隔符上。感谢 Vince Emigh 的建议,他向我展示了我只为换行符和逗号设置了它。因此,当程序扫描输入文件时,它在 290 标记后面包含空格,从而抛出 InputMistmatchException。通过将 in_file.useDelimiter("[,\n]"); 更改为 in_file.useDelimiter("[,\n]\\s"); 它纠正了我遇到的问题。