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
System.out.printf("@%s@\n", name)outside the loop andSystem.out.printf("@%s@\n", entry.getKey())inside the loop and update the question with the output? The@s are to show whitespaces