Fixing HashMap Values Issue in Java Programming

  • Context: Comp Sci 
  • Thread starter Thread starter Quincy
  • Start date Start date
  • Tags Tags
    Java Programming
Click For Summary

Discussion Overview

The discussion revolves around a Java programming issue related to the use of a HashMap to store objects of a custom class, Friend. Participants are examining why all values for the Friend objects in the HashMap appear to be the same, despite different inputs from a text file. The scope includes debugging techniques and object-oriented programming concepts.

Discussion Character

  • Technical explanation
  • Debugging
  • Conceptual clarification

Main Points Raised

  • One participant suggests adding output statements in the while loop to debug what is being stored in the HashMap.
  • Another participant points out that the data members of the Friend class are declared as static, which could lead to all Friend objects sharing the same values.
  • A participant questions whether the print() method is functioning correctly and if the values are being read from the text file properly.
  • It is noted that the static variables in the Friend class are causing the last set of inputs to overwrite the values for all Friend objects in the HashMap.
  • Participants discuss the evolution of debugging tools in Java over the years, indicating that modern IDEs offer better support than in the past.

Areas of Agreement / Disagreement

Participants generally agree that the static nature of the variables in the Friend class is the primary issue causing the observed behavior. However, there are varying suggestions on how to approach debugging and verifying the input values.

Contextual Notes

There is an assumption that the input file is formatted correctly, and the discussion does not resolve whether the print() method is functioning as intended. The implications of using static variables in object-oriented programming are also not fully explored.

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 ·
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
13K