×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

HashMap Problem

HashMap Problem

HashMap Problem

(OP)
Trying to Return the Current and previous values in the HashMap, but is only returning the current keys. Any advise will be much appreciated. Thanks

      The code:

private void saveJButtonActionPerformed( ActionEvent event )
        {
             int x = 0;
            //Creates the tree map and the values that will be placed in the map
            TreeMap hm = new TreeMap();
            hm.put("Average Attendance ", subtotalJTextField.getText());
            hm.put("1)",Name1JTextFeild.getText());
            hm.put("Attendance", Members1JTextFeild.getText());
            hm.put("2)",Name2JLabel.getText());
            hm.put("Attendance", Members2JTextFeild.getText());
            hm.put("3)",Name3JLabel.getText());
            hm.put("Attendance", Members3JTextFeild.getText());
            Set set = hm.entrySet();
            
            Iterator i = set.iterator();
            
            
            //displays the values in the map
            while( i.hasNext())
                {
                
                Map.Entry me = (Map.Entry)i.next();
                DisplayAttendance.setText(me.getKey() + " : " + me.getValue());
                System.out.println(me.getKey() + "  " + me.getValue());
                

                }
Replies continue below

Recommended for you

RE: HashMap Problem

CODE

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class TestMap {
       public static void main(String args[]) {
            int x = 0;
            //Creates the tree map and the values that will be placed in the map
            TreeMap hm = new TreeMap();
            hm.put("Average Attendance ", "zero");
            hm.put("1)","one");
            hm.put("Attendance", "two");
            hm.put("2)","three");
            hm.put("Attendance", "four");
            hm.put("3)","five");
            hm.put("Attendance", "six");
            Set set = hm.entrySet();
            
            Iterator i = set.iterator();
            
            
            //displays the values in the map
            while( i.hasNext())
                {                
                Map.Entry me = (Map.Entry)i.next();
                System.out.println(me.getKey() + "  " + me.getValue());
                }
       }
}
The key "Attendance" has been used for triple times, so the value is replaced a new value at each time. "four" replaces "two", then "six" replaces "four".

RE: HashMap Problem

This forum is for J2EE posts. In future please post pure Java questions to forum269: Java (Sun).

Tim

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close