Results 1 to 15 of 15
Thread: not getting right columns to add
- 12-06-2012, 05:35 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
not getting right columns to add
I'm trying to read a text file (which is working) and adding up a "column" to print a total.
In the text file, a line looks like this-
110001 commercial 500000.00 101
The first number would be a property ID number. The second value is the type of property. The third is the price of the property, and the fourth is the listing agent's ID number.
The file I'm trying to output should look like this -
Total properties listed: 7
Total value of properties listed: 2120000.00
110001
110020
110223
110333
110421
110442
112352
My problem is with the total I'm getting. My program seems to be adding the property IDs (first "column") and I need to add the property's price (3rd "column"). I've been messing around with this for 2 hours and I can't figure out where I've gone wrong. I suspect that it's the way it's been split but I can't be sure. Here's the code -
I know there's a few other things that that need to be fixed like my error catching, but the addition thing is really driving me nuts.Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package my.report; //import packages import java.io.*; import java.util.*; /** * * @author Rusteater */ public class agentReport { public static void main(String[] args) throws FileNotFoundException, IOException { // get file name from user for processing (listings.txt) Scanner console = new Scanner(System.in); System.out.println("Name of file to process: "); String inputFile = console.next(); // create new file (overview.txt) and write data PrintWriter out = new PrintWriter("overview.txt"); File input = new File(inputFile); BufferedReader in = new BufferedReader(new FileReader(input)); //variables for output file int count = 0; double sum = 0; String fileContent; // figure count and total sum for properties try { while ((fileContent = in.readLine()) != null) { count++; String[] propertyList = fileContent.split("[\\s}]"); { sum += Double.parseDouble(propertyList[0]); } } //Print total number of properties listed and the total value System.out.println("Total properties listed: " + count); System.out.println("Total value of properties listed: " + sum + "\n"); } //catch exception catch (IOException | NumberFormatException e) { System.out.println("Error: " + e); } //arrayList for property IDs ArrayList<String> propID = new ArrayList<>(); Scanner identFile = new Scanner((input)); //print property IDs to output file while (identFile.hasNextLine()) { fileContent = identFile.nextLine(); String[] fields = fileContent.split("[\\s}]"); String propertyID = (fields[0]); { propID.add(propertyID); System.out.println(propertyID); } } // flush out and close PrintWriter out.flush(); out.close(); } }
Any help would be much appreciated. Thanks.
- 12-06-2012, 09:34 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: not getting right columns to add
It's simply because you sum the property id. See line number 40 you have something like:
As a suggestion, please debug your code carefully. You can use the IDE debug tool or simply print out why you get the wrong value.Java Code:sum += Double.parseDouble(propertyList[0]);
Last edited by wsaryada; 12-06-2012 at 09:41 AM.
Website: Learn Java by Examples
- 12-06-2012, 10:08 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: not getting right columns to add
And you should be able to do this in a single cycle.
Reading the file twice is unecessary.Please do not ask for code as refusal often offends.
- 12-06-2012, 10:30 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
I can't seem to figure out why I can get the proper token in the line to process.
0 was obviously the ID, 4 is the type (commercial)... shouldn't 6 be the price?
Not sure what I'm doing wrong. The books I have don't seem to really help me out here.
Right now the 4 is reading the word "commercial" out of the line.Java Code:try { while ((fileContent = in.readLine()) != null) { count++; String[] propertyPrice = fileContent.split("[\\s}]"); { sum += Double.parseDouble(propertyPrice[4]); } }
EDIT- I just watched a few videos about Split and the videos show what I thought was how this should work. Price should be the 3rd token[2]. But when I run this it tells me that the string is empty. Really stuck here.Last edited by neveser; 12-07-2012 at 12:29 AM.
- 12-06-2012, 11:56 PM #5
Senior Member
- Join Date
- Jan 2012
- Posts
- 210
- Rep Power
- 2
Re: not getting right columns to add
What do You mean with "shouldn't 6 be the price"?
6 what?
- 12-07-2012, 01:29 AM #6
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
Sorry, should have been more clear.
The [0] in that lines gives me the first position in the line which was the property's ID number.Java Code:sum += Double.parseDouble(propertyPrice[0]);
4 gives me the 2nd token in the line. I wasn't sure why that was because I thought that [1] would have been that.
[2] should be the 3rd token in the line, but it's not. It's an empty string. I started to think that the spaces were being counted that's how I got to 6 for the 3rd token. After watching the tutorial videos, the spaces are not counted (just as I initially thought). I'm not understanding what's going on with that line. Shouldn't 6 return an array out of bounds error since there are only 4 tokens in the line? Why would 4 return the second token in the line?
- 12-07-2012, 03:06 AM #7
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: not getting right columns to add
I believe there more then one spaces between each token. That's why you get more data. From your sample data you should only have 4 data in the array. Please make sure that one one space that separate each token.
Website: Learn Java by Examples
- 12-07-2012, 03:36 AM #8
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
Thanks wsaryada. I didn't know that more than one white spaces made a difference. I thought white space was looked as a whole whether it was 1 or 20 spaces together.
Is there a regular expression that can stand for all white space between tokens?
- 12-07-2012, 03:46 AM #9
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: not getting right columns to add
You can use "\\s+" for the regex.
Website: Learn Java by Examples
- 12-07-2012, 04:55 AM #10
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
Thanks again wsaryada.
I see you have that example right on the main page of your website.
- 12-07-2012, 06:33 AM #11
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
OK, trying to tidy this up a bit. Had to add a few lines to get it to print to the file. File was empty before.
First, where do you catch the exception with the throw in the main class? (not sure if I worded that right)
I got the idea for this line from my book -
When I put in the wrong file name, the programs halts. I don't see how to catch that if it's not in a try block.Java Code:public static void main(String[] args) throws FileNotFoundException, IOException {
Should I put this section in a try block?
Java Code:Scanner console = new Scanner(System.in); System.out.println("Name of file to process: "); String inputFile = console.next();
Second, can you combine these 2 lines to print to the console and write to the file -
Java Code:System.out.println("Total properties listed: " + count); out.println("Total properties listed: " + count);
- 12-07-2012, 09:56 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: not getting right columns to add
Your FileNotFound will be thrown from
You need to think what you want to do if the user puts in a duff file name.Java Code:BufferedReader in = new BufferedReader(new FileReader(input));
If you want to ask them again, then you'll need a try/catch around this and some form of loop to keep asking them.Please do not ask for code as refusal often offends.
- 12-07-2012, 10:52 PM #13
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
I added a simple try catch return around that line and it creates a new error
cannot find symbol variable in
Error is on line 36
I kept playing around with adding a try/catch last night but it kept creating errors in other parts of the code.
At least this time it's only the 1.
EDIT: Actually there are 2 warnings. Didn't see that other one.
exception IOException is never thrown in body of corresponding try statementJava Code:catch (IOException | NumberFormatException e) {
Looking again at the example code in the book, they never catch the exception either. Probably not the best example to give someone to learn from.Last edited by neveser; 12-07-2012 at 11:16 PM.
- 12-08-2012, 12:14 AM #14
Senior Member
- Join Date
- Nov 2012
- Posts
- 228
- Rep Power
- 1
Re: not getting right columns to add
throw the try catch around your reader
try
(all reader method not any calls of this method just the method itself)
catch exception (e or w/e)
println "file not found"
that should work
- 12-08-2012, 12:37 AM #15
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: not getting right columns to add
Here's what I tried around the reader
That's giving me the cannot find symbol variable in error in this lineJava Code:try { BufferedReader in = new BufferedReader(new FileReader(input)); } catch (FileNotFoundException e) { System.err.println("File not found. Please try again."); return; }
EDIT - Decided to hang this one up. No need to catch the exception. If it bombs out, so be it.Java Code:while ((fileContent = in.readLine()) != null) {Last edited by neveser; 12-08-2012 at 08:53 PM.
Similar Threads
-
Prints Asterik in even columns and Minus in odd columns
By Sapana in forum New To JavaReplies: 2Last Post: 11-30-2011, 02:08 PM -
Groupable columns
By new2java2009 in forum New To JavaReplies: 2Last Post: 07-13-2011, 11:51 PM -
2D Array - Sum of columns
By dutchgold92 in forum New To JavaReplies: 2Last Post: 02-28-2011, 07:18 PM -
raw text to columns...
By ashishgaj in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks