Results 1 to 10 of 10
- 05-15-2012, 12:02 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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.
- 05-15-2012, 12:06 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Can't Find my Math Error does anyone see it?
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)?It only involves two values
- 05-15-2012, 12:15 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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.
Here is the TextFileWriter.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; }
and Here is my TextFileReaderJava 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(); }
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); }
- 05-15-2012, 12:54 AM #4
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.
- 05-15-2012, 02:02 AM #5
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.
- 05-15-2012, 02:52 AM #6
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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; }
Thanks for your time and suggestionsJava 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;
- 05-15-2012, 03:10 AM #7
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.
- 05-15-2012, 03:43 AM #8
Member
- Join Date
- Mar 2012
- Posts
- 18
- Rep Power
- 0
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
- 05-15-2012, 05:51 AM #9
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
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-15-2012, 08:00 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Can't Find my Math Error does anyone see it?
Similar Threads
-
jarsiner Error (Error: could not find libjava.so)
By haythame78 in forum Java AppletsReplies: 2Last Post: 08-22-2011, 10:44 AM -
Math.pow returning cannot find symbol
By jmanv888 in forum New To JavaReplies: 6Last Post: 07-09-2011, 07:29 PM -
can any one plz find the error.
By shalini_cmc in forum AWT / SwingReplies: 1Last Post: 03-23-2011, 09:34 AM -
Find the error in this and why this can't be ?
By ron2794 in forum New To JavaReplies: 11Last Post: 12-14-2010, 03:23 PM -
I need help with an error! Cannot find symbol error!
By ambria1975 in forum New To JavaReplies: 2Last Post: 07-07-2010, 01:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks