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


in assignment have work outputting list of names either valid output file or error file. need right scan text file input errors (for example, supposed int has letter in it, therefore throws exception).

my program works fine in detecting these errors , print out valid information, problem outputting invalid information. example, when program detects invalid double input, prints part of line instead of entire line. i'm @ loss right , i'm wondering if can me?

here program...

import java.io.*; import java.util.*; public class taxdeductiondriver {  public static void main(string[] args) {      //array used store information , output net income     employee[] info = new employee[71];                  scanner input = null;     printwriter output = null;     int = 0;       try {         input = new scanner(new fileinputstream("payroll.txt"));         output = new printwriter(new fileoutputstream("error.txt"));              while(input.hasnextline()) {                  try {                      //assigns column each variable                     int empnum = input.nextint();                                            string fname = input.next();                     string lname = input.next();                     double avghrs = input.nextdouble();                     double hrwage = input.nextdouble();                      //checks see if wage below minimum ($10.35)                     if(hrwage < 10.35) {                                                         throw new inputmismatchexception();                     }                      //object used output list of valid information , calculate tax deductions                     //if 1 of parameters null, input mismatch exception thrown                     info[i] = new employee(empnum, fname, lname, avghrs, hrwage);                                 i++;                      }                     catch (inputmismatchexception e) {                         //outputs invalid information error file                         output.println(input.nextline());                              continue;                     }             }             output.close();     }     catch (filenotfoundexception e) {         system.out.println("couldn't find file");     }     {         system.exit(0);     } } } 

my error file supposed 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 run program, ends this

*john connaught 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 idea of what's happening, how printwriter output entire line instead of part of it?

the scanner advance every time call "next" method, in catch block input.nextline() return remaining part of line last "next" call next end of line character.
fix need read whole line first, parse , in case of error, output it. 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 error file     } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -