Results 1 to 15 of 15
  1. #1
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default 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 -

    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();
        }
    }
    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.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    7

    Default Re: not getting right columns to add

    It's simply because you sum the property id. See line number 40 you have something like:

    Java 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.
    Last edited by wsaryada; 12-06-2012 at 09:41 AM.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default 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.

  4. #4
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default 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.
    Java 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.
    Last edited by neveser; 12-07-2012 at 12:29 AM.

  5. #5
    diamonddragon is offline Senior Member
    Join Date
    Jan 2012
    Posts
    210
    Rep Power
    3

    Default Re: not getting right columns to add

    What do You mean with "shouldn't 6 be the price"?
    6 what?

  6. #6
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: not getting right columns to add

    Quote Originally Posted by diamonddragon View Post
    What do You mean with "shouldn't 6 be the price"?
    6 what?
    Sorry, should have been more clear.

    Java 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?

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    7

    Default 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.

  8. #8
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default 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?

  9. #9
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    7

    Default Re: not getting right columns to add

    You can use "\\s+" for the regex.

  10. #10
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: not getting right columns to add

    Thanks again wsaryada.

    I see you have that example right on the main page of your website.

  11. #11
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default 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 -
    Java 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?
    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. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: not getting right columns to add

    Your FileNotFound will be thrown from
    Java 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.
    Please do not ask for code as refusal often offends.

  13. #13
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default 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.
    Java 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.
    Last edited by neveser; 12-07-2012 at 11:16 PM.

  14. #14
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default 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

  15. #15
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: not getting right columns to add

    Here's what I tried around the reader
    Java 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
    Java 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.
    Last edited by neveser; 12-08-2012 at 08:53 PM.

Similar Threads

  1. Replies: 2
    Last Post: 11-30-2011, 02:08 PM
  2. Groupable columns
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 07-13-2011, 11:51 PM
  3. 2D Array - Sum of columns
    By dutchgold92 in forum New To Java
    Replies: 2
    Last Post: 02-28-2011, 07:18 PM
  4. raw text to columns...
    By ashishgaj in forum New To Java
    Replies: 1
    Last Post: 02-02-2011, 06:29 PM

Posting Permissions

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