Java 将CSV文件解析为HashMap将存储空值
我试图读取csv文件并将数据存储到哈希映射中。 我能够正确地添加键,但在添加值时,它会为每个键添加null。我不知道为什么。这是我的密码: 编辑代码:Java 将CSV文件解析为HashMap将存储空值,java,csv,hashmap,bufferedreader,Java,Csv,Hashmap,Bufferedreader,我试图读取csv文件并将数据存储到哈希映射中。 我能够正确地添加键,但在添加值时,它会为每个键添加null。我不知道为什么。这是我的密码: 编辑代码: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.*; public class ExampleActivity { public static HashMap<String,
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class ExampleActivity {
public static HashMap<String, String> hm = new HashMap<String, String>();
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("countries.csv"));
String line;
HashMap<String, String> hm = new HashMap<String, String>();
while ((line = reader.readLine()) != null) {
String str[] = line.split(",");
if (str.length > 1) {
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0].trim(), str[1]);
}
}
//System.out.println(hm);
}
public static void main(String[] args) throws IOException {
readCountry();
Scanner in = new Scanner(System.in);
String l = null;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
// System.out.println("Country Code: " + l + "\nCountry Name: " +
// hm.get(l) );
if (hm.containsKey(l)) {
System.out.println("Country Code: " + l + "\nCountry Name: "
+ hm.get(l));
} else {
System.out.println("Missing key for " + l);
}
}
}
以下是输出的屏幕截图:
注释掉hashmap的方法本地声明,它应该可以正常工作。对代码进行如下更改:
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("d:/countries1.csv"));
String line;
// HashMap<String, String> hm = new HashMap<String, String>(); Remove this line
public static void readCountry()引发IOException{
BufferedReader=new BufferedReader(新文件读取器(“d:/countries1.csv”);
弦线;
//HashMap hm=新建HashMap();删除此行
注释掉hashmap的方法本地声明,它应该可以正常工作。对代码进行如下更改:
public static void readCountry() throws IOException{
BufferedReader reader = new BufferedReader(new FileReader("d:/countries1.csv"));
String line;
// HashMap<String, String> hm = new HashMap<String, String>(); Remove this line
public static void readCountry()引发IOException{
BufferedReader=new BufferedReader(新文件读取器(“d:/countries1.csv”);
弦线;
//HashMap hm=新建HashMap();删除此行
试试这个:
while((line = reader.readLine()) != null){
String str[] = line.split(",");
if (str.size() > 1){
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0], str[1]);
}
}
您的for循环是不必要的
同时看看黑暗骑士对你的空值问题的回答
编辑
您能否将其添加到代码中,并查看其功能:
if (hm.containsKey(l)
System.out.println("Country Code: " + l + "\nCountry Name: " + hm.get(l) );
else
System.out.println("Missing key for " + l);
System.out.println("Printing hashmap");
for(Entry<String, String> entry : hm.entrySet()) {
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
接下来呢
Scanner in = new Scanner(System.in);
String l;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
试试这个:
while((line = reader.readLine()) != null){
String str[] = line.split(",");
if (str.size() > 1){
System.out.println("Data 0: " + str[0]);
System.out.println("Data 1: " + str[1]);
hm.put(str[0], str[1]);
}
}
您的for循环是不必要的
同时看看黑暗骑士对你的空值问题的回答
编辑
您能否将其添加到代码中,并查看其功能:
if (hm.containsKey(l)
System.out.println("Country Code: " + l + "\nCountry Name: " + hm.get(l) );
else
System.out.println("Missing key for " + l);
System.out.println("Printing hashmap");
for(Entry<String, String> entry : hm.entrySet()) {
{
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
接下来呢
Scanner in = new Scanner(System.in);
String l;
System.out.println("Please enter a three letter country:");
l = in.nextLine();
l = l.trim();
使用流API,您可以从以下代码段开始
Path path = Paths.get("countries.txt");
Map<String, String> countries = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty())
.map((Object t) -> ((String) t).split(",", 2))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l.length > 1 ? l[1] : ""));
System.out.println("countries = " + countries);
代码段过滤掉空行,对于没有的行,
将值指定为空字符串
编辑修改后的readCountry
如下所示
public static void readCountry() throws IOException {
Path path = Paths.get("countries.txt");
Map<String, String> hm = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty() && l.contains(","))
.map((Object t) -> ((String) t).split(",", 2))
.peek((String[] l) ->
System.out.printf("Data 0: %s%nData 1: %s%n", l[0], l[1]))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l[1]));
}
使用流API,您可以从以下代码段开始
Path path = Paths.get("countries.txt");
Map<String, String> countries = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty())
.map((Object t) -> ((String) t).split(",", 2))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l.length > 1 ? l[1] : ""));
System.out.println("countries = " + countries);
代码段过滤掉空行,对于没有的行,
将值指定为空字符串
编辑修改后的readCountry
如下所示
public static void readCountry() throws IOException {
Path path = Paths.get("countries.txt");
Map<String, String> hm = Files.lines(path, StandardCharsets.UTF_8)
.filter((String l) -> !l.isEmpty() && l.contains(","))
.map((Object t) -> ((String) t).split(",", 2))
.peek((String[] l) ->
System.out.printf("Data 0: %s%nData 1: %s%n", l[0], l[1]))
.collect(toMap((String[] l) -> l[0],
(String[] l) -> l[1]));
}
为什么要将输入字符串拆分两次?一次在循环外,一次在循环内。我不确定你在csv文件的for loopshare示例中尝试做什么。我将编辑帖子,我最初使用过一次。你当前结构中的for循环没有帮助,一秒钟的问题是csv文件。有一些奇怪的问题单词中的间距(即使看起来正常)。我真的很感谢你的帮助。谢谢。你为什么要将输入字符串拆分两次?一次在循环外,一次在循环内。我不确定你在csv文件的for loopshare示例中尝试做什么。我将编辑帖子,我最初使用过一次。你的for循环在当前结构中没有帮助,一秒钟的问题是e CSV文件。文字中有一些奇怪的不可见空格(尽管看起来很正常)。我真的很感谢你的帮助。谢谢你。没有for循环,我得到了一个越界错误。检查更新,听起来你的CSV文件中有行没有逗号。这是CSV文件的一些错误。我修复了这个问题,但是原始问题(值为null的地方)仍在发生。我也按照黑暗骑士的建议做了。好的,这里打印的是:System.out.println(“数据0:+str[0]);System.out.println(“数据1:+str[1]);您能发布代码的示例输出吗?@DarkNight,有什么进一步的建议吗?它显示“缺少用于…”的密钥。我想它没有正确地添加到哈希映射中。如果没有for循环,我将得到一个越界错误。检查更新,听起来您的CSV文件中的行没有逗号。CSV文件中有一些错误。我修复了该问题,但原始问题(值为null)仍然在发生。我也按照黑暗骑士的建议做了。好吧,这里打印的是:System.out.println(“数据0:+str[0]);System.out.println(“数据1:+str[1]);你能发布你代码的示例输出吗?@DarkKnight,有什么进一步的建议吗?它正在显示“缺少的键…”.我想它没有正确地添加到哈希映射中。