Search code examples
javafile-read

Need help with Reading File Input than Calculating!


I am have a real hard time with this program i have to write. The program has to

Read in from the file

  1. Name
  2. Asking Price
  3. Sale Price
  4. Amount still owed on Mortgage

Print out AND write to a file a table that contains the following:

  • Name
  • Asking Price
  • Mortgage Amount
  • Selling Price
  • Realtor Commission
  • Property Taxes Net price

    Calculations:

  • Realtor Commission = Selling Price * 6%
  • Property Taxes = Selling Price * 10.5%
  • Net Price = Asking Price - Mortgage Amount - Realtor Commission
  • Total Profit / Loss = Accummulate the net prices

i have no clue how to separate the data and do separate calculations!! A sample data file looks like this...

Hopkins 250000      223000      209000
Smith   305699      299999      297500
Forrest 124999      124999      53250
Fitch   214999      202500      200000

There is no way i can read the data then do the calculations and then write the new data to a new file, please help!


Solution

  • try {
        float netTotal = 0;
        String thisLine = null;
        FileOutputStream out = new FileOutputStream("out.txt"); 
        BufferedReader br = new BufferedReader(new FileReader("inputFile.txt"));
        while ((thisLine = br.readLine()) != null) { // while loop begins here
            String[] parts = thisLine.split("[\\t\\n\\r ]+");
            int askingPrice = Integer.parseInt(parts[1]); 
            int salePrice = Integer.parseInt(parts[2]);
            int amountOwed = Integer.parseInt(parts[3]);
            float commission = salePrice * 0.06;
            float tax = salePrice * 0.105;
            float netPrice = askingPrice - amountOwed - commission;
            netTotal += netPrice;
    
            out.write((parts[0] + "\t").getBytes());       //name
            out.write((askingPrice + "\t").getBytes());    //asking price
            out.write((amountOwed + "\t").getBytes());     //mortgage amount
            out.write((salePrice + "\t").getBytes());      //selling price
            out.write((commission + "\t").getBytes());     //realtor commission
            out.write((tax + "\t").getBytes());            //sales tax
            out.write((netPrice + "\t\n").getBytes());     //net price
        }
        out.close(); 
        br.close();
        System.out.println("Net profit/loss:  " + netTotal);
    } 
    catch (Exception e) {
        e.printStackTrace();
    }