Java - 错误,树集中的空字符串

时间:2012-12-08 07:28:32

标签: java string treeset

我一整天都在看这件事,一行一行,我无法弄清楚为什么我得到一个空字符串错误。程序读取文本文件,第一部分的输出是正确的,但是,第二部分给出了空字符串错误。 输出应该看起来像这样 -

COMMERICAL

FARM

LAND

住宅

101 600000.00

105 30000.00

106 200000.00

107 1040000.00

110 250000.00

我看起来像这样 -

COMMERCIAL

FARM

LAND

住宅

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 land 30000.00 105

110442农场200000.00 106

110421 land 40000.00 107

112352住宅250000.00 110

在过去的两天里,我尝试过的事情比我无法计算的更多。 我的选择已经用完了。我真的很感激任何人都能给我的帮助。

最后,这是代码......

        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;
}
}

1 个答案:

答案 0 :(得分: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());
    }
}