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.