0

私は一日中このことを行ごとに調べてきましたが、空の文字列エラーが発生する理由がわかりません。プログラムはテキスト ファイルを読み取り、最初の部分からの出力は正しいのですが、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;
}
}
4

1 に答える 1

0

あなたのpType方法では、スキャナーに特定の区切り文字を使用しました: -

in.useDelimiter("[1234567890. ]");

さて、この行を読むと: -

110001 commercial 500000.00 101

各数値の間に空の文字列が表示されます。事前にチェックしないと、問題が発生します。

たとえば、このサンプル コードを参照してください: -

String str = "110001 commercial 500000.00 101";
Scanner scanner = new Scanner(str);
scanner.useDelimiter("[0-9. ]");

while (scanner.hasNext()) {
    System.out.print("*" + scanner.next() + "*-");
}

出力: -

**-**-**-**-**-**-*commercial*-**-**-**-**-**-**-**-**-**-**-**-**-**-

ご覧のとおり、これらの のペアは**、各数値の間で読み取られた空の文字列を示します。

while追加を避けるために、ループ内に空の文字列のテストを追加できます。

String line = "";
while (in.hasNext()) {
    if (!(line = in.next()).isEmpty()) {
        type.add(line.toUpperCase());
    }
}
于 2012-12-08T07:38:59.863 に答える