Results 1 to 7 of 7
- 12-07-2012, 02:29 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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.
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.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; } }
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.
- 12-07-2012, 03:30 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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.
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.Java Code:catch (Exception e) { }Website: Learn Java by Examples
- 12-07-2012, 09:16 AM #3
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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(); } }
- 12-07-2012, 11:00 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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 -
it cures the warning. Could those warnings be the source of the problem?Java Code:Set<String> type = new TreeSet<String>();
- 12-08-2012, 12:50 AM #5
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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.
This is a sample line from the text file that is being read -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; } }
110001 commercial 500000.00 101Last edited by neveser; 12-08-2012 at 02:19 AM.
- 12-08-2012, 07:43 AM #6
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
Re: Not getting all output from program
From looking at the error codes, the lines they correspond to are
41 -and 84 -Java Code:Set<String> agentReport = agentValue(inputFile);
So I'm going to assume that line 41 is the source of all my grief.Java Code:Double pValue = Double.parseDouble(fields[2]);
Still not sure what's going on there, everything looks good to me.
- 12-08-2012, 07:45 PM #7
Member
- Join Date
- Nov 2012
- Posts
- 40
- Rep Power
- 0
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 08:52 PM.
Similar Threads
-
can you guess the output of this program?
By killutch in forum New To JavaReplies: 4Last Post: 09-18-2012, 09:39 AM -
Need help. I am not able to get the desired display of output in my program.
By pcmechanic in forum New To JavaReplies: 1Last Post: 02-13-2012, 02:51 PM -
Output of the program
By Sheenu Gupta in forum New To JavaReplies: 1Last Post: 07-25-2011, 08:32 AM -
Wrong Output (Java Program)
By poupas in forum New To JavaReplies: 12Last Post: 11-28-2010, 04:28 PM -
Program can run but output all null
By matt_well in forum New To JavaReplies: 15Last Post: 07-24-2008, 08:48 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks