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 -
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();
}
}
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.
Any help would be much appreciated. Thanks.
Re: not getting right columns to add
It's simply because you sum the property id. See line number 40 you have something like:
Code:
sum += Double.parseDouble(propertyList[0]);
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.
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.
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.
Code:
try {
while ((fileContent = in.readLine()) != null) {
count++;
String[] propertyPrice = fileContent.split("[\\s}]");
{
sum += Double.parseDouble(propertyPrice[4]);
}
}
Right now the 4 is reading the word "commercial" out of the line.
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.
Re: not getting right columns to add
What do You mean with "shouldn't 6 be the price"?
6 what?
Re: not getting right columns to add
Quote:
Originally Posted by
diamonddragon
What do You mean with "shouldn't 6 be the price"?
6 what?
Sorry, should have been more clear.
Code:
sum += Double.parseDouble(propertyPrice[0]);
The [0] in that lines gives me the first position in the line which was the property's ID number.
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?
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.
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?
Re: not getting right columns to add
You can use "\\s+" for the regex.
Re: not getting right columns to add
Thanks again wsaryada.
I see you have that example right on the main page of your website.
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 -
Code:
public static void main(String[] args) throws FileNotFoundException, IOException {
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.
Should I put this section in a try block?
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 -
Code:
System.out.println("Total properties listed: " + count);
out.println("Total properties listed: " + count);
Re: not getting right columns to add
Your FileNotFound will be thrown from
Code:
BufferedReader in = new BufferedReader(new FileReader(input));
You need to think what you want to do if the user puts in a duff file name.
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.
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.
Code:
catch (IOException | NumberFormatException e) {
exception IOException is never thrown in body of corresponding try statement
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.
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
Re: not getting right columns to add
Here's what I tried around the reader
Code:
try {
BufferedReader in = new BufferedReader(new FileReader(input));
} catch (FileNotFoundException e) {
System.err.println("File not found. Please try again.");
return;
}
That's giving me the cannot find symbol variable in error in this line
Code:
while ((fileContent = in.readLine()) != null) {
EDIT - Decided to hang this one up. No need to catch the exception. If it bombs out, so be it.