I have been going over this thing all day, line by line and I cannot figure out why I’m getting an empty String error. The program reads a text file and the output from the first part is correct, however, the second part gives me the empty String error.
Output is supposed to look like this –
COMMERICAL
FARM
LAND
RESIDENTIAL
101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00
Mine looks like this –
COMMERCIAL
FARM
LAND
RESIDENTIAL
java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.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 block of errors repeats itself 7 times which happens to be the number of lines in the text file.
This is what the text file looks like –
110001 commercial 500000.00 101
110223 residential 100000.00 101
110020 commercial 1000000.00 107
110333 land 30000.00 105
110442 farm 200000.00 106
110421 land 40000.00 107
112352 residential 250000.00 110
I have tried more things than I can count in the past 2 days to no avail.
I’m running out of options. I’d really appreciate any help that anyone could give me.
Finally, here’s the code…
package my.report;
import java.io.*;
import java.util.*;
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) {
e.printStackTrace();
}
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> agentReport = agentValue(inputFile);
// Print agent IDs and values
for (String tail : agentReport) {
{
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<>();
Scanner in = new Scanner(new File(inputFile));
// use while loop and delimiter to select specific characters for set
in.useDelimiter("[1234567890. ]");
while (in.hasNext()) {
type.add(in.next().toUpperCase());
}
in.close();
return type;
}
// read file and print out 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 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;
}
}
In your
pTypemethod, you have used certain delimiters for scanner: –Now, when you read this line: –
You will get an empty string between the each numeric value. Which will give you some problems, if you don’t pre-check it.
See this sample code for e.g: –
Output: –
As you can see those pairs of
**, which denotes the empty string read between each numeric values.You can add a test for empty string inside the
whileloop, to avoid adding it: –