简体   繁体   中英

How can I print out entire String line to output file (java)?

In my assignment I have to work with outputting a list of names to either a valid output file or an error file. What I need to do right now is scan a text file for any input errors (for example, what is supposed to be an int has a letter in it, therefore throws an exception).

My program works fine in detecting these errors and print out the valid information, but my problem is outputting the invalid information. For example, when my program detects an invalid double input, it only prints part of the line instead of the entire line. I'm at loss right and I'm wondering if you can help me?

Here is my program...

import java.io.*;
import java.util.*;
public class TaxDeductionDriver {

public static void main(String[] args) {

    //array will be used to store information and output net income
    Employee[] info = new Employee[71];             
    Scanner input = null;
    PrintWriter output = null;
    int i = 0;


    try {
        input = new Scanner(new FileInputStream("payroll.txt"));
        output = new PrintWriter(new FileOutputStream("error.txt"));

            while(input.hasNextLine()) {

                try {

                    //assigns a column to each variable
                    int empNum = input.nextInt();                       
                    String fName = input.next();
                    String lName = input.next();
                    double avgHrs = input.nextDouble();
                    double hrWage = input.nextDouble();

                    //checks to see if wage is below minimum ($10.35)
                    if(hrWage < 10.35) {                                
                        throw new InputMismatchException();
                    }

                    //object used to output list of valid information and calculate tax deductions
                    //if one of parameters is null, input mismatch exception is thrown
                    info[i] = new Employee(empNum, fName, lName, avgHrs, hrWage);           

                    i++;

                    }
                    catch (InputMismatchException e) {
                        //outputs invalid information to error file
                        output.println(input.nextLine());     
                        continue;
                    }
            }
            output.close();
    }
    catch (FileNotFoundException e) {
        System.out.println("Couldn't find file");
    }
    finally {
        System.exit(0);
    }
}
}

my error file is supposed to be like this

> >Error lines found in file payroll
16783 JOHN CONNAUGHT 30.5 10.00
21O15 JAMES HAROLD 32.0 10.50
52726 MITCHELL HACKETT 23,7 12.05
#9331 MARIO TODARO 35.0 17.00
22310 CLAUDIA O’HARE 30.5 9.80
26734 EDITH ROOSEVELT 20.O 25.50
41024 EMILE BOURGEOYS 8.5 6.45
43018 JAMES WALKER 20.0 8.00
00812 VICTORIA PORTER 36.0 9.50
9201( DAVID BROCK 29.0 22.50

But when I run the program, it ends up like this

*john connaught is missing here*
21O15 JAMES HAROLD 32.0 10.50
23,7 12.05
#9331 MARIO TODARO 35.0 17.00

20.O 25.50



9201( DAVID BROCK 29.0 22.50

I have an idea of what's happening, but how do I get the PrintWriter to output the entire line instead of part of it?

The Scanner will advance every time you call a "next" method, so in your catch block input.nextLine() will return the remaining part of the line from the last "next" call to the next end of line character.
To fix this you need to read the whole line first, parse it and in case of error, output it. Something like this:

    String line = input.nextLine();
    try {
        String[] tokens = line.split("\\s+");
        int empNum = Integer.parseInt(tokens[0]);
        String fName = tokens[1];
        String lName = tokens[2];
        double avgHrs = Double.parseDouble(tokens[3]);
        double hrWage = Double.parseDouble(tokens[4]);
        //.......
    }
    catch (InputMismatchException e) {
        output.println(line);     //outputs invalid information to error file
    }

Try changing this line:

output.println(input.nextLine());

to:

output.println(empNum +" fname" +" lname"+ avghrs + ""+  hrswage);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM