即使存在键,Java HashMap也会返回空值(Java HashMap returning null values even when key is present)

编程入门 行业动态 更新时间:2024-10-28 14:26:58
即使存在键,Java HashMap也会返回空值(Java HashMap returning null values even when key is present)

我正在开发一个项目,我们在Map插入键和值对。

如果密钥存在于Map ,我的代码将返回该密钥的值。

但是,即使密钥存在, HashMap也不会返回预期值。

首先,我从文件中读取键和值对,然后我读取另一个与第一个文件具有几乎相同键的文件。

然后我返回一些键的值,但对于其中许多键,值为null 。

这里是我的代码片段:

Scanner scanner = new Scanner(new FileReader("a.txt")); LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); while (scanner.hasNextLine()) { String[] columns = scanner.nextLine().split(";"); map.put(columns[0], columns[1]); } System.out.println(map); for (Map.Entry<String, String> entry : map.entrySet()) { // name is the input of second file if (entry.getKey().equals(name)) { num = entry.getValue(); fun(num); } }

我的输入文件是

ABC; 1

DEF; 2

GHI; 3

......而名字将是abc

高清

I am working on a project in which we insert key and value pairs in a Map.

If the key is present in the Map, my code returns the value for that key.

However, the HashMap is not returning the expected values, even though the key is present.

First I read the key and value pairs from a file, then I read another file which has almost the same keys as the first file.

Then I return values for some keys, but for many of them the value is null.

Here is a snippet of my code:

Scanner scanner = new Scanner(new FileReader("a.txt")); LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); while (scanner.hasNextLine()) { String[] columns = scanner.nextLine().split(";"); map.put(columns[0], columns[1]); } System.out.println(map); for (Map.Entry<String, String> entry : map.entrySet()) { // name is the input of second file if (entry.getKey().equals(name)) { num = entry.getValue(); fun(num); } }

My input file is

abc;1

def;2

ghi;3

... and name will be abc

def

最满意答案

正在比较的字符串可能包含空格,换行符,换行符。

正如我们之前所怀疑的那样,你的输入文件train.txt尾随空格,导致地图查找失败。 除此之外,您的Java代码还有许多冗余代码块。 我把它清理干净了。 这是您修改后的代码:

import java.io.*; import java.util.*; public class ExampleClass1 { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new FileReader("trainnames.txt")); LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); while (scanner.hasNextLine()) { String[] columns = scanner.nextLine().split(";"); map.put(columns[0].trim(), columns[1].trim()); } scanner.close(); System.out.println("******** map is: " + map); File file = new File("onn.csv"); // output file FileWriter fileWriter = new FileWriter(file); scanner = new Scanner(new FileReader("train.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.charAt(0) == '>') { //System.out.println("==== line: [" + line + ']'); String num = map.get(line); no(num, fileWriter); } } scanner.close(); fileWriter.close(); } public static void no(String num, FileWriter fileWriter) throws IOException { fileWriter.append(num + ',' + System.getProperty("line.separator")); System.out.println(num); } }

Strings that are being compared might have white space, linefeed, newline characters.

As we suspected earlier your input file train.txt has man trailing white-spaces and that is causing map lookup to fail. Besides that your Java code had many redundant block of code. I have cleaned it up. Here is your modified code:

import java.io.*; import java.util.*; public class ExampleClass1 { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new FileReader("trainnames.txt")); LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); while (scanner.hasNextLine()) { String[] columns = scanner.nextLine().split(";"); map.put(columns[0].trim(), columns[1].trim()); } scanner.close(); System.out.println("******** map is: " + map); File file = new File("onn.csv"); // output file FileWriter fileWriter = new FileWriter(file); scanner = new Scanner(new FileReader("train.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); if (line.charAt(0) == '>') { //System.out.println("==== line: [" + line + ']'); String num = map.get(line); no(num, fileWriter); } } scanner.close(); fileWriter.close(); } public static void no(String num, FileWriter fileWriter) throws IOException { fileWriter.append(num + ',' + System.getProperty("line.separator")); System.out.println(num); } }

更多推荐

本文发布于:2023-08-03 10:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1386702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:也会   returning   Java   HashMap   key

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!