Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java-错误,树集合中的空字符串_Java_String_Treeset - Fatal编程技术网

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

Java-错误,树集合中的空字符串,java,string,treeset,Java,String,Treeset,我一整天都在一行一行地检查这个问题,我不明白为什么会出现空字符串错误。程序读取一个文本文件,第一部分的输出是正确的,但是第二部分给出了空字符串错误。 输出应该是这样的- 商业的 农场 土地 住宅 java.lang.NumberFormatException: empty String at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011) at java.lang.Double.pa

我一整天都在一行一行地检查这个问题,我不明白为什么会出现空字符串错误。程序读取一个文本文件,第一部分的输出是正确的,但是第二部分给出了空字符串错误。 输出应该是这样的-

商业的

农场

土地

住宅

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)
101600000.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

在过去的两天里,我尝试了很多我无法计数的事情,但都没有成功。 我别无选择了。我真的很感激任何人能给我的帮助

最后,这里是代码

        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;
}
}
打包my.report;
导入java.io.*;
导入java.util.*;
公共类代理报告{
公共静态void main(字符串[]args)
抛出FileNotFoundException{
//从用户处获取输入(文件名)
扫描仪控制台=新扫描仪(System.in);
System.out.print(“要处理的文件名:”);
字符串inputFile=console.next();
BufferedWriter pwfo=null;
试一试{
pwfo=new BufferedWriter(new FileWriter(“C:\\agentReport.txt”,true));
}捕获(IOE异常){
e、 printStackTrace();
}
PrintWriter pwo=新的PrintWriter(pwfo);
//构造树集(属性类型)
设置propertyType=pType(inputFile);
//打印属性类型
for(字符串类型:propertyType){
系统输出打印项次(类型);
pwo.println(类型);
}
//构造树集(代理ID和值)
设置agentReport=agentValue(inputFile);
//打印代理ID和值
用于(字符串尾部:agentReport){
{
系统输出打印项次(尾部);
pwo.println(尾部);
}
}
pwo.flush();
pwo.close();
}
//以大写字母读取输入和按字母顺序排列的属性类型
公共静态集pType(字符串inputFile)抛出FileNotFoundException//Construct treeSet以返回属性类型
{
设置类型=新树集();
扫描仪输入=新扫描仪(新文件(输入文件));
//使用while循环和分隔符为集合选择特定字符
in.useDelimiter(“[1234567890.]”);
while(在.hasNext()中){
键入.add(in.next().toUpperCase());
}
in.close();
返回类型;
}
//读取文件并打印出代理ID和属性值
公共静态设置代理值(字符串输入文件)
抛出FileNotFoundException{
树集尾部=新树集();
SortedMap agentValue=新树映射();
扫描仪输入=新扫描仪(新文件(输入文件));
弦线;
while(在.hasNextLine()中){
试一试{
line=in.nextLine();
字符串[]字段=line.split([\\s+]);
字符串agentId=(字段[3]);
Double pValue=Double.parseDouble(字段[2]);
if(agentValue.containsKey(agentId)){
pValue+=agentValue.get(agentId.doubleValue();
}
agentValue.put(agentId,pValue);
//使用键和值创建keyMap
}捕获(例外e){
e、 printStackTrace();
}
Set keySet=agentValue.keySet();
用于(字符串键:键集){
Number value=agentValue.get(键);
System.out.println(键+“:”+值);
tail.add(键+“:”+值);
}
}
返回尾;
}
}

在您的
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());
    }
}

哇!我完全错过了。我想这不会影响代码的第二部分。删除分隔符会极大地更改我的输出。这是不正确的,但还有很多。当我在while下添加if语句时,scanner部分给了我一个错误-找不到符号变量scanner-不确定如何补救。@neveser。对不起,
scanner
是扫描器的参考资料,我通常使用。您已在中使用了
,因此将
scanner.next()
替换为.next()中的
Ahh。。。我懂了。现在,我的输出由5个代理ID和属性类型组成,然后是7个错误块。所以这里仍然有空字符串。不知何故,财产价值没有达到应有的水平。我今天一直在玩这个游戏,没有取得任何进展。在我没有看到的代码的后半部分中肯定有错误。无法修复空字符串错误。好的,有些人指出了我的许多其他问题。我想我现在明白了,我试过了。我是Java新手,所以我并不擅长使用它,就我所知,它并没有帮助我解决这个问题。