InputMismatchException when scanning from a file

时间:2018-03-23 00:07:35

标签: java java.util.scanner java-io

I am writing a program that takes information from an input file (First and last name, hourly rate, and hours worked) calculates the net pay and writes it to a different file. The file wages.txt has the following:

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

The output should be:

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

The overtime rate is half of the hourly rate. The rate is a double and the hours worked is an int. In my tests the output format line gets wrote to the file, and the exception is thrown as the program gathers the information for calculating pay.

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();
    }
}

}

When ex.printStackTrace() is ran the console says:

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)

1 个答案:

答案 0 :(得分:0)

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