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 .getValue in IF statement (or leading + on int value)

807600Nov 28 2007 — edited Nov 28 2007
I could get this code to work using an ArrayList, but I'm having difficulties getting it to work properly with a HashMap. I have users add keys and values (string and int respectively) into a HashMap in another method. That works fine, I can display them and 'search' them which is what I need.

But what I need now is a way to display them properly. As you can tell by my code below, I basically want to say that if the Value (that's the integer value) is greater than 0, I want to add in a leading "+" to the number. This way when I display the keys and values, it doesn't come with "Paris: GMT +-1" for example, if the user puts in "Paris" for a key and "-1" for the value (time zones).
  public void printAllZones()
  {
      Set set = hm.entrySet();
      Iterator i = set.iterator();
      System.out.print("\f");
      System.out.println("Time zones are:");
      while (i.hasNext()){
          Map.Entry me = (Map.Entry)i.next();
          
          if(me.getValue() > 0) {
          System.out.println(me.getKey() + ": GMT +" + me.getValue() );
          }
          else {
          System.out.println(me.getKey() + ": GMT " + me.getValue() );
          }
      }
  }
I thought maybe that .containsValue would work, but that'd check all Values, so wouldn't really work in this situation.

Any ideas? Thanks!!

(also, for the record, this code doesn't compile in my program because I get an error that you can't apply the operator > to java.lang.Object,int which is me.getValue().)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 26 2007
Added on Nov 28 2007
3 comments
749 views