我一整天都在检查这件事,一行一行,我不知道为什么我得到一个空字符串错误。该程序读取一个文本文件,第一部分的输出是正确的,但是,第二部分给了我空字符串错误。输出应该是这样的 -
商业的
农场
土地
住宅
101 600000.00
105 30000.00
106 200000.00
107 1040000.00
110 250000.00
我的看起来像这样-
商业的
农场
土地
住宅
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)
该错误块重复自身 7 次,恰好是文本文件中的行数。这是文本文件的样子 -
110001 商业 500000.00 101
110223 住宅 100000.00 101
110020 商业 1000000.00 107
110333 土地 30000.00 105
110442 农场 200000.00 106
110421 土地 40000.00 107
112352 住宅 250000.00 110
在过去的 2 天里,我尝试了更多的东西,但无济于事。我的选择已经不多了。我真的很感激任何人可以给我的任何帮助。
最后,这是代码...
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;
}
}