Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HashMap keys and values not staying consistent (my fault, but why?)

807600Nov 28 2007 — edited Nov 28 2007
Alright, after starting out for the very first time with HashMaps a few hours ago, I've gotten the hang of them decently I think. I am encountering a problem, though, when I try to get user-selected keys + values to display. My code is:
   public void useZone(String use)
   {
       Object used = hm.get(use);
       System.out.print("\f");
       Set set = hm.entrySet();
       Iterator i = set.iterator();

       if (used != null){
           System.out.println("Time Zone Used:");
           Map.Entry thiszone = (Map.Entry)i.next();
           Object usedzone = thiszone.getValue(); //Get the value of the zones and assign them as Object usedzone
           Integer useval = (Integer) usedzone; //Assign the Object to an Integer value so we can use it
           difference = useval;
           
           if (useval > 0){ //Display the hour offset correctly
               System.out.println(use + ": GMT +" + usedzone.toString());
           }
           else if (useval == 0){
               System.out.println(use + ": GMT");
           }
           else if (useval < 0){
               System.out.println(use + ": GMT " + usedzone.toString());
            }
        
       else{
           System.out.println("Invalid zone name.");
       }
       } //End if
       ClockDisplay.getZone(difference); //Send variable difference
  }
What will happen is that the first time I will be fine...

Say the HashMap keys and values are

"Rome", "-2"
"Chicago", "6"
"New York", "5"
"Paris", "-1"

If I type "Rome" in when I execute this method, it gives me the correct output - "Rome: GMT -2"

If I then re-run the method, as to display a different one, I get "Chicago: GMT -2"

It's like the name the user inputs is updating, obviously, but the value of the HashMap that goes along with the user-input doesn't match up or something. My code is confusing myself after looking at it for nearly 10 hours, so I apologize that I had to post something so trivial here - I'm sure it's just a problem with my wordings....any help would be greatly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2007
Added on Nov 28 2007
4 comments
96 views