Comp Sci Fixing HashMap Values Issue in Java Programming

  • Thread starter Thread starter Quincy
  • Start date Start date
  • Tags Tags
    Java Programming
AI Thread Summary
The issue with the HashMap in the Java program arises from the use of static variables in the Friend class, which causes all instances to share the same id, lat, and lon values. As a result, when multiple Friend objects are created, they overwrite these static values, leading to all keys in the HashMap displaying the same last set of coordinates. To resolve this, the variables should be made instance variables instead of static ones, ensuring that each Friend object maintains its own state. Debugging output statements can help verify the values being stored in the HashMap. Modern IDEs provide enhanced debugging tools that can assist in identifying such issues more effectively.
Quincy
Messages
228
Reaction score
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
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
 
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?
 
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.)
 
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.
 

Similar threads

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