Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Java programming

  1. May 27, 2010 #1
    Code (Text):
    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?
     
  2. jcsd
  3. May 27, 2010 #2

    Mark44

    Staff: Mentor

    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 (Text):

    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
     
  4. May 28, 2010 #3

    Mark44

    Staff: Mentor

    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?
     
  5. Jun 1, 2010 #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.)
     
  6. Jul 9, 2010 #5

    Borg

    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    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 (Text):

    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.
    Yes, dramatically. Today's IDEs are pretty awesome.

    Edit: Ugh. I just realized how old this thread was.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook