7 Replies Latest reply: Jan 18, 2013 5:57 AM by gimbal2 RSS

    Problem with Iterator

    985610
      i have an homework to make a GUI address book with name and Phonenumber to add remove next previous the contacts. i tried to use the hash map but coulnot go through the contacts.
        • 1. Re: Problem with Iterator
          EJP
          Why not?
          • 2. Re: Problem with Iterator
            985610
            i couldnot print the contact..
            import java.awt.Container;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import java.util.ArrayList;
            import java.util.HashMap;
            import java.util.List;
            import java.util.Vector;

            import javax.swing.JButton;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JOptionPane;
            import javax.swing.JTextField;


            //GUI
            public class Addressbook<T> extends JFrame implements ActionListener {
                 HashMap<String, String> contacts = new HashMap<String, String>();
                 private JTextField stname, stphone;
                 
                 private JButton ad, b, c, d, e;
                 private int index;
                 
                 private JLabel tempLbl,tempLbl1,tempLbl2,tempLbl3;
                 
                 
                 
                 
                 public Addressbook() {
                      
                      
                      tempLbl = new JLabel("Name:");
                      stname = new JTextField();
                      tempLbl1 = new JLabel("Phone:");
                      stphone = new JTextField();
                      ad = new JButton("Add");
                      b = new JButton("Delete");
                      c = new JButton("Next");
                      d = new JButton("Previous");          
                      tempLbl2 = new JLabel("Total contacts=");
                      tempLbl3 = new JLabel();
                      index = 0;

                      ad.addActionListener(this);
                      b.addActionListener(this);
                      c.addActionListener(this);
                      d.addActionListener(this);          

                      Container pane = getContentPane();

                      pane.setLayout(new GridLayout(10, 0));
                      pane.add(tempLbl3);
                      pane.add(tempLbl);
                      pane.add(stname);
                      pane.add(tempLbl1);
                      pane.add(stphone);          
                      pane.add(ad);
                      pane.add(b);
                      pane.add(c);
                      pane.add(d);
                      pane.add(tempLbl2);
                      
                 
                 

                      setTitle("Addressbook");
                      setSize(320, 320);
                      setVisible(true);
                 }

                 public void actionPerformed(ActionEvent z) {
                      String name = stname.getText();
                      String phoneNumber = stphone.getText();
                      name = name.toUpperCase();           
            //add          
            if(z.getSource() == ad){          
                           Contact.put(name, phoneNumber);     
                                     
                      }
            //delete
                      if(z.getSource() == b){          
                           Contact.remove(phoneNumber);
                           Contact.remove(name);
                      }
                      
                                
                                //get size
                                int s = Contact.size();
                                tempLbl2.setText(String.format("", s));
                                
                                //click next
                           if(z.getSource() == c){
                                index++;
                                Contact p = Contact.get(name,phoneNumber);
                                tempLbl3.setText(String.format("Contact = %.2f", p));
                                // dont know what to do
                                }
            //next button disabled
                                if(Contact.size() == index){
                                     
                                     c.setEnabled(false);
                                }
                                
                                //click previous
                                if(z.getSource() == d){
                                index--;
                                }
            //previous button disabled
                                if(index < 0){
                                     
                                     d.setEnabled(false);
                                }
                                
                                
                                
                                //sort the list
                                Collections.sort(Contact);
                           

                 }
                 

                 public static void main(String[] args) {
                      new Addressbook();
                 }

            }

            Edited by: 982607 on Jan 17, 2013 9:35 PM

            Edited by: 982607 on Jan 17, 2013 9:37 PM

            Edited by: 982607 on Jan 17, 2013 10:05 PM
            • 3. Re: Problem with Iterator
              r035198x
              If you have to post code then please use code tags.
              It's hard to see what you are tying to do as it is. You declared a HashMap but then also tried to create a Vector? Which one are you using? Vector or Map? They are different and have different methods so you can't just substitute one for the other.
              • 4. Re: Problem with Iterator
                985610
                thanks for your concern i have modified it now!! I am newbie to java and all i want to make is a address book to add, delete, next, previous conacts with name and phoneNumber. Hoping for your help
                • 5. Re: Problem with Iterator
                  r035198x
                  982607 wrote:
                  I am newbie to java and all i want to make is a address book to add, delete, next, previous conacts with name and phoneNumber. Hoping for your help
                  The best way is to learn from the compiler. Try to compile your code and look at the errors that come up. Those error messages will teach you a lot about Java including indicating what you need to change to make your code work. Only post if you get stuck with a particular error and in that case post the error message and line of code reported by the error.
                  • 6. Re: Problem with Iterator
                    EJP
                    Posting all this code doesn't answer my question in the slightest. Why can't you iterate over your map? What is the actual question here?
                    • 7. Re: Problem with Iterator
                      gimbal2
                      982607 wrote:
                      thanks for your concern i have modified it now!! I am newbie to java
                      Okay, but that's not an argument for anything. Lots of people want to use the fact that they're "new" to something as a reason to trigger people to act as teachers towards them. But all you're saying is "I'm new and I need to be doing lots of studying".
                      and all i want to make is a address book to add, delete, next, previous conacts with name and phoneNumber.
                      Excellent! Having a learning project is an incredibly efficient way to learn as you're working towards something in stead of just trying to learn random knowledge.
                      Hoping for your help
                      You'll have to be more specific. What kind of help do you need that you can't get by doing research. What do you already know? Where are you stuck? If you don't know much of anything about Java yet, so you don't even know the basics of the language, then a good book is your first stop and not the internet.