Fixing HashMap Values Issue in Java Programming

In summary: I had no recollection of this whatsoever. Turns out my comment was completely off-base. There is no way to fix the key/id mismatch without rewriting the code. Sorry about that.In summary, the hash map in the Friend class is not correctly storing the key and the id values. This mismatch causes the print() method to not output the correct values for each key.
  • #1
Quincy
228
0
Code:
import java.util.*;
import java.io.*;

public class Friend
{
	private static int id;
	private static double lat;
	private static double lon;

	public Friend(int id, double lat, double lon)
	{
		this.id = id;
		this.lat = lat;
		this.lon = lon;
	}
	
	.
        .
        .
        .
	
	public void print()
	{
		System.out.println(this.id + " " + this.lat + " " + this.lon);
	}
	
	public static void main(String[] args)
	{
		File file = new File("small_world.txt");

		try {
		Scanner scan = new Scanner(file);
		HashMap<Integer,Friend> friend_list = new HashMap<Integer,Friend>(); 
		int id_number = 0;

		
		while(scan.hasNext())
		{
			id_number = scan.nextInt();
		Friend friend = new Friend(id_number, scan.nextDouble(),scan.nextDouble());

			
			friend_list.put(id_number, friend);
			
		}
		for (Integer key : friend_list.keySet())
		{
			System.out.println(key);
			friend_list.get(key).print();
		}
		
		}
		
		catch (FileNotFoundException e)
		{
			System.out.print(e);
		}
	}
}

The output is:

1
5 79.99 179.99
2
5 79.99 179.99
3
5 79.99 179.99
4
5 79.99 179.99
5
5 79.99 179.99

It's supposed to be:

1
1 0.0 0.0
2
2 10.1 -10.1
3
3 -12.2 12.2
4
4 38.3 38.3
5
5 79.99 179.99

The input file is a text file containing:

1 0.0 0.0
2 10.1 -10.1
3 -12.2 12.2
4 38.3 38.3
5 79.99 179.99

Why are all the values for each key in the HashMap the same as the last value that was put into it? I've noticed it's only like that for the values but not the keys, the keys all in proper order. How do I fix this problem?
 
Physics news on Phys.org
  • #2
There's nothing that jumps out at me, so it's time to put on your debugging hat. It's been 15 years since I wrote any Java code, and if I recall, there wasn't much in the way of debugging tools at that time. Hopefully, the situation has changed.

If not, I would add some output statements in the while loop to see what was actually being stored in friend_list
Code:
while(scan.hasNext())
{
    id_number = scan.nextInt();
    Friend friend = new Friend(id_number, scan.nextDouble(),scan.nextDouble());
    // For debugging purposes.
    System.out.println("ID    lat    lon");
    friend.print();

    friend_list.put(id_number, friend);
}
This should give you some insight on why you are always getting the lat and lon values for the 5th item in your hash table.

HTH
 
  • #3
I didn't notice this before (it was pointed out to my by Hurkyl), the data members of your Friend class are all static. Is there some reason for doing this?
 
  • #4
I never liked helping people with code because I'm afraid to say silly things like this:

Are you sure you're actually going through your text file and getting the values instead of printing the last variables stored? Check out your print() method, I think that is where your problem might be. It seems logical because your data is getting into the text file, it simply isn't being printed out properly.(Be careful, that might have been nonsense.. although I'm confident I knew what I was talking about.)
 
  • #5
As Mark44 states, the static variables are why you are getting the last set of inputs for all of the Friend objects.

To understand why the key and Friend.id values don't match up, look at what you are printing out here:
Code:
for (Integer key : friend_list.keySet())
{
    System.out.println(key);
    friend_list.get(key).print();
}
The key value and the Friend id value are not stored in the same place. While they started out the same, the static variables in Friend are causing its version of the number to get overwritten each time that a new Friend object is created.
Mark44 said:
It's been 15 years since I wrote any Java code, and if I recall, there wasn't much in the way of debugging tools at that time. Hopefully, the situation has changed.
Yes, dramatically. Today's IDEs are pretty awesome.

Edit: Ugh. I just realized how old this thread was.
 

FAQ: Fixing HashMap Values Issue in Java Programming

How do I fix a HashMap values issue in Java?

To fix a HashMap values issue in Java, you can use the put() method to replace the incorrect value with the correct value. Alternatively, you can use the remove() method to remove the incorrect value and then add the correct value using the put() method.

Why are my HashMap values not updating correctly?

If your HashMap values are not updating correctly, it could be due to using the same key for multiple values. HashMaps do not allow duplicate keys, so the value associated with the key will be overwritten. Make sure each key has a unique value in order to update correctly.

How can I check if a value exists in a HashMap in Java?

You can use the containsValue() method to check if a specific value exists in a HashMap. This method returns a boolean value of true if the value is found and false if it is not found.

What is the difference between HashMap and Hashtable in Java?

HashMap and Hashtable are both data structures used to store key-value pairs. The main difference is that HashMap is not synchronized, meaning it is not thread-safe, while Hashtable is synchronized, making it thread-safe. Additionally, Hashtable does not allow null keys or values, while HashMap allows one null key and multiple null values.

Is it possible to iterate through a HashMap in Java?

Yes, it is possible to iterate through a HashMap in Java. You can use the keySet() method to get a set of all the keys in the HashMap, and then use a for loop to iterate through the set and retrieve the corresponding values using the get() method.

Similar threads

Replies
7
Views
2K
Replies
1
Views
1K
Replies
7
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
2
Views
2K
Replies
16
Views
4K
Replies
2
Views
13K
Back
Top