Search code examples
javafileinputioexceptionprintwriter

program that uses input file and creates a new one


I'm writing a code that uses an input file called InvetoryReport.txt in a program I am supposed to create that is supposed to take this file, and then multiply two pieces of data within the file and then create a new file with this data. Also at the beginning of the program it is supposed to ask you for the name of the input file. You get three chances then it is to inform you that it cannot find it and will now exit, then stop executing.

My input file is this

       Bill     40.95        10
       Hammer     1.99         6
       Screw      2.88         2
       Milk     .03    988

(The program is supposed to multiply the two numbers in the column and create a new column with the sum, and then under print another line like this " Inventory Report

      Bill   40.95      10    409.5
       Hammer      1.99       6     11.94
       Screw       2.88       2       5.76
       Milk      .03      988       29.64
      Total INVENTORY value     $ 456.84"

and my program I have so far is this

 package textfiles;
 import java.util.Scanner;
 import java.io.PrintWriter;
 import java.io.IOException;

 public class LookOut{
 double total = 0.0;

 String getFileName(){
     System.out.printIn("Type in file name here.");
         try {
               int count =1;
               FileReader fr = new FileReader("InventoryReport.txt");
               BufferedReader br = new BufferedReader(fr);
               String str;        
               while ((str = br.readLine()) != null) {                 
                out.println(str + "\n");
                }
                br.close();
                } catch (IOException e) {
                     if(count == 3) {
                         System.out.printIn("The program will now stop executing.");
                         System.exit(0);
                         count++;
                         }
                 }

         return str;
 }
 void updateTotal(double d){
     total = total + d;
 }
 double getLineNumber(int String_line){
     String [] invRep = line.split(" ");
     Double x = double.parseDouble(invRep[1]);
     Double y = double.parseDouble(invRep[2]);
     return x * y;
 }
 void printNewData(String = newData) {
 PrintWriter pW = new PrintWriter ("newData");
 pw.print(newData);
 pw.close;
 }    

 public static void main(String[] args){
    String str = ("Get file name"); 
    String str = NewData("InventoryReport/n");
    File file = new File(str);
    Scanner s = new Scanner(file);
      while(s.hasNextLine()) {
         String line = s.nextLine();
         double data = getLineNumber(line);
         update total(data);
         NewData += line + " " + data + "/n";
         Print NewData(NewData);
 }
 }
 }

I'm getting multiple error codes that I just cant seem to figure out.


Solution

  •  try {
         int count =1;
         FileReader fr = new FileReader("InventoryReport.txt");
         BufferedReader br = new BufferedReader(fr);
         String str;
    
            while ((str = br.readLine()) != null) {
    
                br.close();
               } catch (IOException e) {
                if(count == 3) {
                     System.out.printIn("The program will now stop executing.");
                    System.exit(0);
                    count++;
                    }
               }
    

    Despite your best intentions you are in fact missing a '}'. Note that you haven't escaped the Try block before the catch. I imagine this is because you confused the closing } for the while statement as the closing } for the try block. Do this instead:

    try {
         int count =1;
         FileReader fr = new FileReader("InventoryReport.txt");
         BufferedReader br = new BufferedReader(fr);
         String str;
    
            while ((str = br.readLine()) != null) {
    
                br.close();
               }
             }
             catch (IOException e) {
                if(count == 3) {
                     System.out.printIn("The program will now stop executing.");
                    System.exit(0);
                    count++;
                    }
               }
    

    Also, your indentation is ALL OVER THE PLACE. This should be a lesson to you in why you should format your code properly! It is so easy to miss simple syntax errors like that if you're not formatting properly. It's also hard for others to read your code and figure out what's wrong with it.