Results 1 to 10 of 10
  1. #1
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Can't Find my Math Error does anyone see it?

    My application reads from a text file, then edits the information, then saves it back. Somewhere along the line I made a mathematical error. I am not having very much luck finding it. It only involves two values.

    I don't know where my hours and Fees are getting screwed up, or out of order but it's frustrating!

    Java Code:
    package FileService;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileWriter {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileWriter(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public void writeAllRecords(List<LinkedHashMap<String, String>> rawData) throws IOException {
    
            File file = new File(filepath);
            String formattedData = formatter.encodeALL(rawData);
    
            PrintWriter txtWriter = new PrintWriter(
                    new BufferedWriter(
                    new FileWriter(file, false)));
    
    
            txtWriter.println(formattedData);
            txtWriter.close();
        }
    
    package FileService;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileReader {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileReader(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public String getFilepath() {
            return filepath;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public GarageFileFormat getFormatter() {
            return formatter;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public List<LinkedHashMap<String, String>> getAllRecords() {
            File file = new File(filepath);
            String rawDataFromFile = "";
    
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(file));
                String line = in.readLine();
                while (line != null) {
                    rawDataFromFile += (line + "\n");
                    line = in.readLine();  // strips out any carriage return chars
                }
    
            } catch (IOException ioe) {
                System.out.println("Houston, we have a problem! reading this file");
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            return formatter.decodeAll(rawDataFromFile);
    
        }
    
    
    
    import FileService.GarageFileFormat;
    import FileService.TextFileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class Recepit {
        
        TextFileWriter w = new TextFileWriter("src/new.txt", new GarageFileFormat());
        private GarageInfo garageInfo;
        private GarageInfo[] gDb;
        private Transaction[] transactions= new Transaction[0];
        private double hours;
        private double feeAmount;
        
        public Recepit(String garageId) {
            if (gDb == null) {
                gDb = new GarageInfo[2];
                GarageInfo one = new GarageInfo("100", "Milwaukee", "WI");
                GarageInfo two = new GarageInfo("200", "Madison", "WI");
                gDb[0] = one;
                gDb[1] = two;
            }
            garageInfo = this.findGarage(garageId);
        }
        
        public void addItem(String garageId, double hours) {
            Transaction item = new Transaction(garageId, hours);
            Transaction[] temp = new Transaction[transactions.length + 1];
            System.arraycopy(transactions, 0, temp, 0, transactions.length);
            temp[temp.length - 1] = item;
            transactions = temp;
        }
        
        public double getBillRunningTotal() {
            double total = 0;
            
            for (Transaction transaction : transactions) {
                total += transaction.getFeeAmount();
                }
            return (feeAmount +=total);
        }
        
        public void setFeesToDate(double fee){
            this.feeAmount = fee;
        }
        public double getHoursRunningTotal() {
            double total = 0;
            
            for (Transaction transaction : transactions) {
                total += transaction.getHoursParked();
            }
            
            return (hours +=total);
        }
        
        public void setHoursToDate(double hours){
            this.hours = hours;       
        }
        
        public String getRecepitDataAsString() throws IOException {
            String output = "Garage ID: " + garageInfo.getGarageId() + "\n"
                    + "State: " + garageInfo.getCity() + "\n"
                    + "City: " + garageInfo.getState() + "\n\n";
            
            output += "GarageID    Hours    Cost\n";
            output += "============================\n";
            
            for (Transaction c1 : transactions) {
                output += c1.getLineItemAsString() + "\n";
            }
            
            output += "\n =========================\n";
            output += "Lifetime Totals For Garage\n";
            output += "Total Sales: " + (getBillRunningTotal()) + "\n";
            output += "Total Hours: " + (getHoursRunningTotal()) + "\n";
            
            double allFees = getBillRunningTotal();
            double allHours = getHoursRunningTotal();
            
            String convertedFees = Double.toString(allFees);
            String convertedHours = Double.toString(allHours);
            
            List<LinkedHashMap<String, String>> data =
                    new ArrayList<LinkedHashMap<String, String>>();
              
            LinkedHashMap<String, String> rec1 = new LinkedHashMap<String, String>();
            
            rec1.put("totHours", convertedHours);
            data.add(rec1);
            rec1 = new LinkedHashMap<String, String>();
            rec1.put("totFees", convertedFees);
            data.add(rec1);
            w.writeAllRecords(data);
            
            
            return output;
        }
        
        public GarageInfo getGarage() {
            return garageInfo;
        }
        
        public void setGarage(GarageInfo garageInfo) {
            this.garageInfo = garageInfo;
        }
        
        private GarageInfo findGarage(String garageId) {
            GarageInfo garage = null;
            
            for (GarageInfo g : gDb) {
                if (garageId.equals(g.getGarageId())) {
                    garage = g;
                    break;
                }
            }
            
            return garage;
        }
    Last edited by skybeorn; 05-15-2012 at 12:05 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can't Find my Math Error does anyone see it?

    It only involves two values
    We're lazy here! Could you tell us which values? And what the problem is (ie describe the observed vs intended behaviour when the program is run)?

  3. #3
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Can't Find my Math Error does anyone see it?

    My Apologies ;-)

    This is the method that writes the values to disk. I have commented on the Values i was referencing.

    Java Code:
     public String getRecepitDataAsString() throws IOException {
            String output = "Garage ID: " + garageInfo.getGarageId() + "\n"
                    + "State: " + garageInfo.getCity() + "\n"
                    + "City: " + garageInfo.getState() + "\n\n";
             
            output += "GarageID    Hours    Cost\n";
            output += "============================\n";
             
            for (Transaction c1 : transactions) {
                output += c1.getLineItemAsString() + "\n";
            }
             
            output += "\n =========================\n";
            output += "Lifetime Totals For Garage\n";
            output += "Total Sales: " + (getBillRunningTotal()) + "\n";
            output += "Total Hours: " + (getHoursRunningTotal()) + "\n";
             
    //here are the 2 Values. allFees and allHours.
    //I don't know what's happening but it's confusing me. 
            double allFees = getBillRunningTotal();
            double allHours = getHoursRunningTotal();
             
            String convertedFees = Double.toString(allFees);
            String convertedHours = Double.toString(allHours);
             
            List<LinkedHashMap<String, String>> data =
                    new ArrayList<LinkedHashMap<String, String>>();
               
            LinkedHashMap<String, String> rec1 = new LinkedHashMap<String, String>();
             
            rec1.put("totHours", convertedHours);
            data.add(rec1);
            rec1 = new LinkedHashMap<String, String>();
            rec1.put("totFees", convertedFees);
            data.add(rec1);
            w.writeAllRecords(data);
             
             
            return output;
        }
    Here is the TextFileWriter.
    Java Code:
    package FileService;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileWriter {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileWriter(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public void writeAllRecords(List<LinkedHashMap<String, String>> rawData) throws IOException {
    
            File file = new File(filepath);
            String formattedData = formatter.encodeALL(rawData);
    
            PrintWriter txtWriter = new PrintWriter(
                    new BufferedWriter(
                    new FileWriter(file, false)));
    
    
            txtWriter.println(formattedData);
            txtWriter.close();
        }
    and Here is my TextFileReader
    Java Code:
    package FileService;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.LinkedHashMap;
    import java.util.List;
    
    public class TextFileReader {
    
        private String filepath;
        private GarageFileFormat formatter;
    
        public TextFileReader(String filepath, GarageFileFormat formatter) {
            this.filepath = filepath;
            this.formatter = formatter;
        }
    
        public String getFilepath() {
            return filepath;
        }
    
        public void setFilepath(String filepath) {
            this.filepath = filepath;
        }
    
        public GarageFileFormat getFormatter() {
            return formatter;
        }
    
        public void setFormatter(GarageFileFormat formatter) {
            this.formatter = formatter;
        }
    
        public List<LinkedHashMap<String, String>> getAllRecords() {
            File file = new File(filepath);
            String rawDataFromFile = "";
    
            BufferedReader in = null;
            try {
                in = new BufferedReader(new FileReader(file));
                String line = in.readLine();
                while (line != null) {
                    rawDataFromFile += (line + "\n");
                    line = in.readLine();  // strips out any carriage return chars
                }
    
            } catch (IOException ioe) {
                System.out.println("Houston, we have a problem! reading this file");
            } finally {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            return formatter.decodeAll(rawDataFromFile);
    
        }

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Can't Find my Math Error does anyone see it?

    Do you use an IDE? IDEs feature line by line debugging - I suggest you set a break point early in the file, and trace through step by step. At each hop, look at the state of the variables - you should be able to see where the math is going wrong quickly if you know what the result of each operation should be.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Can't Find my Math Error does anyone see it?

    Or add printlns to print out the values of variables as used and changed.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Can't Find my Math Error does anyone see it?

    I have been using NetBeans for an IDE. I found my mistake.

    In the one below my problem was because of lines 15,16,18,19. I was calling the method(s) twice, which was doubling the hour and fee amount.


    Java Code:
    public String getRecepitDataAsString() throws IOException {
            String output = "Garage ID: " + garageInfo.getGarageId() + "\n"
                    + "State: " + garageInfo.getCity() + "\n"
                    + "City: " + garageInfo.getState() + "\n\n";
              
            output += "GarageID    Hours    Cost\n";
            output += "============================\n";
              
            for (Transaction c1 : transactions) {
                output += c1.getLineItemAsString() + "\n";
            }
              
            output += "\n =========================\n";
            output += "Lifetime Totals For Garage\n";
    
            output += "Total Sales: " + (getBillRunningTotal()) + "\n";
            output += "Total Hours: " + (getHoursRunningTotal()) + "\n";
               
            double allFees = getBillRunningTotal();
            double allHours = getHoursRunningTotal();
              
            String convertedFees = Double.toString(allFees);
            String convertedHours = Double.toString(allHours);
              
            List<LinkedHashMap<String, String>> data =
                    new ArrayList<LinkedHashMap<String, String>>();
                
            LinkedHashMap<String, String> rec1 = new LinkedHashMap<String, String>();
              
            rec1.put("totHours", convertedHours);
            data.add(rec1);
            rec1 = new LinkedHashMap<String, String>();
            rec1.put("totFees", convertedFees);
            data.add(rec1);
            w.writeAllRecords(data);
              
              
            return output;
        }

    Java Code:
        public String getRecepitDataAsString() throws IOException {
    //Moved these to top
            double allFees = getBillRunningTotal();
            double allHours = getHoursRunningTotal();
            
            String output = "Garage ID: " + garageInfo.getGarageId() + "\n"
                    + "State: " + garageInfo.getCity() + "\n"
                    + "City: " + garageInfo.getState() + "\n\n";
            
            output += "GarageID    Hours    Cost\n";
            output += "============================\n";
            
            for (Transaction c1 : transactions) {
                output += c1.getLineItemAsString() + "\n";
            }
            
            output += "\n =========================\n";
            output += "Lifetime Totals For Garage\n";
    //Instead of calling the Method to get Totals just sub in the variables.
            output += "Total Sales: " + (allFees) + "\n";
            output += "Total Hours: " + (allHours) + "\n";
            
            
            String convertedFees = Double.toString(allFees);
            String convertedHours = Double.toString(allHours);
            
            List<LinkedHashMap<String, String>> data =
                    new ArrayList<LinkedHashMap<String, String>>();
              
            LinkedHashMap<String, String> rec1 = new LinkedHashMap<String, String>();
            
            rec1.put("totHours", convertedHours);
            data.add(rec1);
            rec1 = new LinkedHashMap<String, String>();
            rec1.put("totFees", convertedFees);
            data.add(rec1);
            w.writeAllRecords(data);
            
            
            return output;
    Thanks for your time and suggestions

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Can't Find my Math Error does anyone see it?

    Where did you post the program's output that showed those doubled values?
    All you said was: hours and Fees are getting screwed up

    If the bad output with the errors and the correct output had been posted, someone could have seen that the values were twice what they should have been.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    skybeorn is offline Member
    Join Date
    Mar 2012
    Posts
    18
    Rep Power
    0

    Default Re: Can't Find my Math Error does anyone see it?

    I'm new to posting on this forum, i'll try to answer all of the questions next time. And post the req'd items

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default Re: Can't Find my Math Error does anyone see it?

    Here's a couple of resources that'll always ensure that you get better help sooner, on this or any other forum:
    How to ask questions the smart way
    SSCCE (Short, Self Contained, Compilable and Executable) example

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Can't Find my Math Error does anyone see it?

    Quote Originally Posted by skybeorn View Post
    I'm new to posting on this forum, i'll try to answer all of the questions next time. And post the req'd items
    It's the trying that's important ... and appreciated. Asking the right questions is *not* easy. If it were we would ask ourselves the right questions and our problems would largely vanish. Darryl's links are good and contain some internet "wisdom".

Similar Threads

  1. jarsiner Error (Error: could not find libjava.so)
    By haythame78 in forum Java Applets
    Replies: 2
    Last Post: 08-22-2011, 10:44 AM
  2. Math.pow returning cannot find symbol
    By jmanv888 in forum New To Java
    Replies: 6
    Last Post: 07-09-2011, 07:29 PM
  3. can any one plz find the error.
    By shalini_cmc in forum AWT / Swing
    Replies: 1
    Last Post: 03-23-2011, 09:34 AM
  4. Find the error in this and why this can't be ?
    By ron2794 in forum New To Java
    Replies: 11
    Last Post: 12-14-2010, 03:23 PM
  5. I need help with an error! Cannot find symbol error!
    By ambria1975 in forum New To Java
    Replies: 2
    Last Post: 07-07-2010, 01:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •