简体   繁体   English

如何将整个String行打印到输出文件(java)?

[英]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). 我现在需要做的是扫描文本文件中是否有任何输入错误(例如,应该是int的内部有一个字母,因此会引发异常)。

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? 我对正在发生的事情有一个了解,但是我如何让PrintWriter输出整个行而不是其中的一部分?

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. 每次您调用“下一个”方法时,扫描程序都会前进,因此在catch块输入中。nextLine()将从上次“下一个”调用返回行的剩余部分到下一个行尾字符。
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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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