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.
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.
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.
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.
Re: Not getting all output from program
OK, I'm getting an empty String in this block -
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();
}
}
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 -
Code:
Set<String> type = new TreeSet<String>();
it cures the warning. Could those warnings be the source of the problem?
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.
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
Re: Not getting all output from program
From looking at the error codes, the lines they correspond to are
41 - Code:
Set<String> agentReport = agentValue(inputFile);
and 84 - 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.
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.