0

I want it to skip any lines read that throw an exception so I store the lines with errors in an ArrayList. But when it goes through when it gets an error, it will not skip the line and give same error and keep repeating.

/**
 * 
 */

/**
 * @author Matt
 *
 */
import java.io.*;
import java.util.*;

public class Receipt {
	BufferedReader br = null;
	
	public Receipt(String fileName) {		
		try {
			br = new BufferedReader(new FileReader(fileName));
		}
		catch(FileNotFoundException fnfe) {
			System.out.println(fnfe.getMessage());
			System.exit(1);
		}
		catch(IOException ioe) {
			System.out.println(ioe.getMessage());
			System.exit(1);
		}
	}
	
	public void BuildReceipt() {
		PrintWriter out = null;
		ArrayList<Integer> errorLines = new ArrayList<Integer>();
		String record;
		boolean prog = true;
		int count = 0;
		int lineNumber = 0;
		while(prog) {
			try {
				out = new PrintWriter(new FileWriter("receipt.txt"));
				String []info;
				int quantity = 0;
				String itemName;
				count = 0;
				float costPerItem = 0;
				double total = 0;
				double tax;
				boolean errorFree;
				
				while ((record = br.readLine()) != null) {
                    errorFree = true;
                    lineNumber ++;
                    for (int i = 0; i < errorLines.size(); i++) {
                        if (errorLines.get(i) == lineNumber) {
                            errorFree = false;
                        }
                    }
                    if (errorFree) {
                        System.out.println(lineNumber);
                        info = record.split("\\s");
                        quantity = Integer.parseInt(info[0]);
                        itemName = info[1];
                        costPerItem = Float.parseFloat(info[2]);
                        total += (costPerItem * quantity);
                        out.write("Item:\t" + itemName + " Item Cost: $\t" + costPerItem + " Quantity:\t" + quantity + " Total Item Cost: $\t" + (costPerItem * quantity));
                        out.println();
                        count++;
                        out.flush();
                    }
                 
                }
				if(record == null) prog = false;
				tax = total * .08;
				out.write("\nTax on purchase:\t$\t" + tax + "\nTotal purchase cost:\t$\t" + (total + tax));
				out.flush();
			}
			catch(NumberFormatException nfe) {
				System.out.println(nfe.getMessage() + " NFE");
				errorLines.add(lineNumber);
			}
			catch(InputMismatchException ime) {
				System.out.println("input mismatch");
				errorLines.add(lineNumber);
			}
			catch(IOException ioe) {
				System.out.println(ioe.getMessage() + " IOE");
				errorLines.add(lineNumber);
				System.exit(1);
			}
			finally {
				try {
					if(br != null) br.close();
				}
				catch(IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}
	}
}

Here is the contents of the text file I'm reading:

5 Chair 12.49
3 Table 34.95
-2 Notebook 699.95
2 Dresser -193.44
1 Bed abc.de
0 Cabinet 233.54
abc Nightstand def.gh
4 Frame 0.00
aaa Shelf 12.44
1
Contributor
1
Reply
2
Views
8 Years
Discussion Span
Last Post by agfras
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.