Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By wsaryada

Thread: Not getting all output from program

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

    Default Not getting all output from program

    It must be something with the last part of the program itself or where I have placed it. Not sure which.

    Java Code:
    package my.report;
    
    import java.io.*;
    import java.util.*;
    
    /**
     *
     * @author Rusteater
     */
    public class agentReport {
    
        public static void main(String[] args)
                throws FileNotFoundException {
    
            // get input from user (file name)
            Scanner console = new Scanner(System.in);
            System.out.print("Name of file to process: ");
            String inputFile = console.next();
            BufferedWriter pwfo = null;
            try {
                pwfo = new BufferedWriter(new FileWriter("C:\\agentReport.txt", true));
            } catch (IOException e) {
            }
            PrintWriter pwo = new PrintWriter(pwfo);
    
            //Construct treeSet (property type)
            Set<String> propertyType = pType(inputFile);
    
            // Print property types 
            for (String type : propertyType) {
                System.out.println(type);
                pwo.println(type);
            }
    
            //Construct treeSet (agent IDs and values) 
            Set<String> agentRpt = agentValue(inputFile);
    
            // Print agent IDs and values 
            for (String tail : agentRpt) {
    
                {
                    System.out.println(tail);
                    pwo.println(tail);
                }
            }
            pwo.flush();
            pwo.close();
        }
    
        // read input and alphabetized property types in uppercase
        public static Set<String> pType(String inputFile)
                throws FileNotFoundException //Construct treeSet to return property types
        {
            Set<String> type = new TreeSet<String>();
            Scanner in = new Scanner(new File(inputFile));
    
            // Use delimiters to select specific chars for set
            in.useDelimiter("[1234567890. ]");
    
            while (in.hasNext()) {
                type.add(in.next().toUpperCase());
            }
            in.close();
    
            return type;
        }
    
        //  read input and print agent ID's and property values
        public static Set<String> agentValue(String inputFile)
                throws FileNotFoundException {
    
            TreeSet<String> tail = new TreeSet<String>();
            SortedMap<String, Number> agentValue = new TreeMap<String, Number>();
            Scanner in = new Scanner(new File(inputFile));
            String line = inputFile;
    
            while (in.hasNextLine()) {
                try {
                    line = in.nextLine();
                    String[] fields = line.split("[\\s}]");
                    String agentId = (fields[3]);
                    Double pValue = Double.parseDouble(fields[2]);
    
                    if (agentValue.containsKey(agentId)) {
                        pValue += agentValue.get(agentId).doubleValue();
                    }
                    agentValue.put(agentId, pValue);
                } 
                catch (Exception e) {
                }
            }
            // Create keyMap with all keys and values
            Set<String> keySet = agentValue.keySet();
            for (String key : keySet) {
                Number value = agentValue.get(key);
                System.out.println(key + ":" + value);
                tail.add(key + ":" + value);
            }
            return tail;      
        }
    }
    I have some warnings in there (no errors), redundant argument use diamond operators and assigned value is never used but I don't think those would stop the program from printing out.

    The output I'm getting is this -

    COMMERCIAL
    FARM
    LAND
    RESIDENTIAL

    What I'm looking for is this -

    COMMERICAL
    FARM
    LAND
    RESIDENTIAL

    101 600000.00
    105 30000.00
    106 200000.00
    107 1040000.00
    110 250000.00

    Both the output from the console and the agentReport.txt file are the same.

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

    Default Re: Not getting all output from program

    There must be an error in your program but you didn't see it. Because you are doing this in your code.

    Java Code:
    catch (Exception e) {
    }
    You have an empty catch block. So if there is any error occurs in your program you know nothing about it. Please never use an empty catch block like this. A least you print the exception stack trace there.
    Fubarable likes this.

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

    Default Re: Not getting all output from program

    OK, I'm getting an empty String in this block -
    Java Code:
     while (in.hasNextLine()) {
                try {
                    line = in.nextLine();
                    String[] fields = line.split("[\\s+]");
                    String agentId = (fields[3]);
                    Double pValue = Double.parseDouble(fields[2]);
    
                    if (agentValue.containsKey(agentId)) {
                        pValue += agentValue.get(agentId).doubleValue();
                    }
                    agentValue.put(agentId, pValue);
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }

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

    Default Re: Not getting all output from program

    I've tried to play around with that block of code and nothing seems to make a difference.
    Looking at it, it seems ok to me. Maybe the error is in a different spot. I can't figure out where though. Everything looks good to me other than those warnings about using the diamond operator. I noticed that if I remove String from the TreeSet in this line -
    Java Code:
    Set<String> type = new TreeSet<String>();
    it cures the warning. Could those warnings be the source of the problem?

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

    Default Re: Not getting all output from program

    OK, I cleared up the diamond operator warnings and it made no difference.
    I moved the block of code in question up into the while loop and it made no difference.

    The error(s) still remain -

    java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at my.report.agentReport.agentValue(agentReport.java: 84)
    at my.report.agentReport.main(agentReport.java:41)

    That repeats 7 times which is the same number of lines in the text file that its creating the TreeSet from.
    This first section of code is doing exactly what it needs to, it's the whole second half that I can't figure out where I've gone wrong.
    This is what that half looks like now.
    Java Code:
    //  read input and print agent ID's and property values
        public static Set<String> agentValue(String inputFile)
                throws FileNotFoundException {
    
            TreeSet<String> tail = new TreeSet<>();
            SortedMap<String, Number> agentValue = new TreeMap<>();
            Scanner in = new Scanner(new File(inputFile));
            String line;
    
            while (in.hasNextLine()) {
                try {
                    line = in.nextLine();
                    String[] fields = line.split("[\\s+]");
                    String agentId = (fields[3]);
                    Double pValue = Double.parseDouble(fields[2]);
    
                    if (agentValue.containsKey(agentId)) {
                        pValue += agentValue.get(agentId).doubleValue();
                    }
                    agentValue.put(agentId, pValue);
                    // Create keyMap with all keys and values
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Set<String> keySet = agentValue.keySet();
                for (String key : keySet) {
                    Number value = agentValue.get(key);
                    System.out.println(key + ":" + value);
                    tail.add(key + ":" + value);
                }
            }
    
            return tail;
        }
    }
    This is a sample line from the text file that is being read -
    110001 commercial 500000.00 101
    Last edited by neveser; 12-08-2012 at 03:19 AM.

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

    Default Re: Not getting all output from program

    From looking at the error codes, the lines they correspond to are
    41 -
    Java Code:
    Set<String> agentReport = agentValue(inputFile);
    and 84 -
    Java Code:
    Double pValue = Double.parseDouble(fields[2]);
    So I'm going to assume that line 41 is the source of all my grief.
    Still not sure what's going on there, everything looks good to me.

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

    Default Re: Not getting all output from program

    Someone pointed out to me last night that the delimiter was removing the information I was missing.
    Removing it gives me this -

    Name of file to process: listings.txt
    100000.00
    1000000.00
    101
    105
    106
    107
    110
    110001
    110020
    110223
    110333
    110421
    110442
    112352
    200000.00
    250000.00
    30000.00
    40000.00
    500000.00
    java.lang.NumberFormatException: empty String
    COMMERCIAL
    FARM
    LAND
    RESIDENTIAL
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at my.report.agentReport.agentValue(agentReport.java: 87)
    at my.report.agentReport.main(agentReport.java:41)
    java.lang.NumberFormatException: empty String

    That block of empty String errors is still getting repeated 7 times. So it does see 7 lines but I guess it sees them as empty?

    EDIT: Someone has pointed out my many errors with this. I think I've got it under control.
    Last edited by neveser; 12-08-2012 at 09:52 PM.

Similar Threads

  1. can you guess the output of this program?
    By killutch in forum New To Java
    Replies: 4
    Last Post: 09-18-2012, 10:39 AM
  2. Replies: 1
    Last Post: 02-13-2012, 03:51 PM
  3. Output of the program
    By Sheenu Gupta in forum New To Java
    Replies: 1
    Last Post: 07-25-2011, 09:32 AM
  4. Wrong Output (Java Program)
    By poupas in forum New To Java
    Replies: 12
    Last Post: 11-28-2010, 05:28 PM
  5. Program can run but output all null
    By matt_well in forum New To Java
    Replies: 15
    Last Post: 07-24-2008, 09:48 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
  •