Java 从txt读取并添加到treeset

Java 从txt读取并添加到treeset,java,bufferedreader,treeset,Java,Bufferedreader,Treeset,我需要读取一个txt文件并将数据存储到树集 public class UrbanPopulationStatistics { private Set<UrbanPopulation> popSet; private File file; private BufferedReader br; public UrbanPopulationStatistics(String fileName) throws IOException { this.popSet = new Tr

我需要读取一个txt文件并将数据存储到树集

public class UrbanPopulationStatistics {

private Set<UrbanPopulation> popSet;
private File file;
private BufferedReader br;

public UrbanPopulationStatistics(String fileName) throws IOException {

    this.popSet = new TreeSet<>();

    readFile("population.txt");
}

private void readFile(String fileName) throws IOException {


    try {
        br = new BufferedReader(new FileReader(fileName));
         String line;
        while ((line=br.readLine()) != null) {


            String[] array = line.split("/");

            popSet.add(new UrbanPopulation(array[0], Integer.parseInt(array[1]), Integer.parseInt(array[4])));

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    br.close();
}

@Override
public String toString() {
    String s = popSet.toString().replaceAll(", ", "");
    return "UrbanPopulationStatistics:\n" + s.substring(1, s.length() - 1) + "\n";
}


public static void main(String[] args) throws IOException {
    UrbanPopulationStatistics stats = new UrbanPopulationStatistics("population.txt");
    System.out.println(stats);
}

}
公共类城市人口统计{
私有集popSet;
私有文件;
专用缓冲读取程序br;
public UrbanPopulationStatistics(字符串文件名)引发IOException{
this.popSet=新树集();
readFile(“population.txt”);
}
私有void readFile(字符串文件名)引发IOException{
试一试{
br=新的BufferedReader(新的文件读取器(文件名));
弦线;
而((line=br.readLine())!=null){
String[]数组=line.split(“/”);
add(新的UrbanPopulation(数组[0],Integer.parseInt(数组[1]),Integer.parseInt(数组[4]);
}
}捕获(IOE异常){
e、 printStackTrace();
}
br.close();
}
@凌驾
公共字符串toString(){
字符串s=popSet.toString().replaceAll(“,”,”);
返回“UrbanPopulationStatistics:\n”+s.substring(1,s.length()-1)+“\n”;
}
公共静态void main(字符串[]args)引发IOException{
UrbanPopulationStatistics=新的UrbanPopulationStatistics(“population.txt”);
系统输出打印项次(统计);
}
}

我曾尝试将缓冲读取器读取的内容转换为数组,然后将其添加到我的树集中,但我得到了错误:线程“main”java.lang.UnsupportedOperationException:不受支持。

代码的问题是没有存储从缓冲区读取的内容(因此从缓冲区读取两次)。您需要分配在变量中读取的内容以检查null,如下所示:

    private void readFile(String fileName) throws IOException {

        try {
            br = new BufferedReader(new FileReader(fileName));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] array = line.split("/");

                popSet.add(new UrbanPopulation(array[0], Integer.parseInt(array[1]), Integer.parseInt(array[4])));

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            br.close();
        }
    }

另外,我会关闭finally块中的BufferedReader以避免资源泄漏。

代码的问题在于没有存储从缓冲区读取的内容(因此从缓冲区读取了两次)。您需要分配在变量中读取的内容以检查null,如下所示:

    private void readFile(String fileName) throws IOException {

        try {
            br = new BufferedReader(new FileReader(fileName));
            String line = null;
            while ((line = br.readLine()) != null) {
                String[] array = line.split("/");

                popSet.add(new UrbanPopulation(array[0], Integer.parseInt(array[1]), Integer.parseInt(array[4])));

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            br.close();
        }
    }

此外,我还将关闭finally块中的BufferedReader以避免资源泄漏。

Integer.parseInt.(数组[4])处的
parseInt
之后有一个额外的句点


编写代码时要小心。语法错误不会“很好地”显示出来,也就是说,在大多数情况下,错误消息不是很有用。不过,它确实显示了错误的大致位置。

Integer.parseInt.(数组[4])处的
parseInt
之后还有一个额外的句点


编写代码时要小心。语法错误不会“很好地”显示出来,也就是说,在大多数情况下,错误消息不是很有用。但它确实显示了错误的大致位置。

我试图使用您的代码重现错误,但没有发生。你的代码没问题

UnsupportedOperationException
是尝试在集合中添加元素时可能发生的异常


但是
TreeSet
实现了add方法。

我试图使用您的代码重现错误,但没有发生。你的代码没问题

UnsupportedOperationException
是尝试在集合中添加元素时可能发生的异常



但是
TreeSet
实现了add方法。

我可能需要添加,文本文件中的每一行都有5个元素,以“/”分隔。我需要第一个元素,它是一个字符串,第二个也是最后一个元素,它们是数字。你说“但这对我来说似乎不起作用”是什么意思?正在引发异常?意外输出?我在popSet.add-part处收到错误。它表示Expected当您将错误添加到问题和读取文件的方式时,您将跳过一行您必须声明的每个循环
String line=“”在while循环之前,while条件应该如下所示:
while((line=br.readLine())!=null){…
并从循环内部删除this
line=br.readLine();
,文本文件中的每一行都有5个元素,以“/”分隔.我需要第一个元素,它是字符串,第二个也是最后一个元素,它们是数字。你说的“但这似乎对我不起作用”是什么意思?引发异常?意外输出?我在popSet.add-part上收到一个错误。它说Expected将错误添加到问题和您读取文件的方式后,您在while循环之前的每个循环中跳过一行,您必须声明
String line=“”;
,while条件应该如下所示
while((line=br.readLine())!=null){…
并从循环内部删除this
line=br.readLine();
谢谢,我在尝试将数组添加到其他人建议的树集时仍然遇到标识符问题,这可能是因为编译问题(由于Integer.parseInt中的额外点)。()。尝试修复。我已使用编译错误的修复程序更新了代码。我也更新了它。现在我收到以下消息:线程“main”中出现异常java.lang.UnsupportedOperationException:尚不受支持。为什么要使用TreeSet?请尝试使用HashSet。如果要使用TreeSet,UrbanPopulation应该为TreeSet实现可比较的接口以维护排序顺序。谢谢,在尝试将数组添加到t时,我仍然遇到标识符问题Reeseta如其他人所建议的,这可能是由于编译问题(由于Integer.parseInt.()中的额外点)。请尝试修复该问题。我已使用编译错误修复程序更新了代码。我也更新了它。现在我收到以下消息:线程“main”中出现异常java.lang.UnsupportedOperationException:尚不受支持。为什么要使用TreeSet?请尝试使用HashSet。如果要使用TreeSet,UrbanPopulation应该为TreeSet实现可比较的接口以维护排序顺序。您在另一个
parseInt
上犯了同样的错误。来吧,注意一下上。我确实删除了句号!!你在另一个
parseInt
上犯了同样的错误。来吧,付点钱