私は一日中このことを行ごとに調べてきましたが、空の文字列エラーが発生する理由がわかりません。プログラムはテキスト ファイルを読み取り、最初の部分からの出力は正しいのですが、2 番目の部分では空の文字列エラーが表示されます。出力は次のようになります-
コマーシャル
農場
土地
居住の
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;
}
}