gpt4 book ai didi

java - 将文件输入添加到树形图

转载 作者:行者123 更新时间:2023-12-02 01:46:22 26 4
gpt4 key购买 nike

我无法将文件输入添加到我的 map 中。它说我缺少一些东西并且 Items [] 没有实例化。我似乎无法弄清楚

public class BigCities {

private Map<String, Set<CityItem>> countryMap;
private File file;


public BigCities(String fileName) {
countryMap = new TreeMap<>();
file = new File(fileName);
readFile(fileName);
}

private void readFile(String fileName) {
// Opg 3c implementeres her.

CityItem cityItem;

try(BufferedReader br = new BufferedReader(new FileReader(fileName))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
String[] items;

while (line != null) {

sb.append(line);
sb.append(System.lineSeparator());
line.split(";");
line = br.readLine();
cityItem = new CityItem(items[1], items[2], items[3]);
}
String everything = sb.toString();
System.out.println(everything);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


public String toString() {
return countryMap.toString();
}


/**
* @param args
*/
public static void main(String[] args) {

//Vaelg ANSI eller UTF8 afhaengig af hvad der virker bedst paa din computer:
BigCities bc = new BigCities("EuroCities ANSI.txt");
//BigCities bc = new BigCities("EuroCities UTF8.txt");
System.out.println(bc);
}

}

我不知道添加输入时缺少什么,但希望有人提供一些输入。我是编程新手,因此我觉得很困惑,因为我觉得我遵循了所有正确的方法。

最佳答案

您永远不会初始化 items 数组,因此当尝试访问它时,您会得到 null,或者它可能会被编译器捕获并在那里给您一个错误。

我怀疑您的意思是将拆分分配给项目,因此更改行

line.split(";");

items = line.split(";");

关于java - 将文件输入添加到树形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57451953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com