how to check if an object already exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • oll3i
    Contributor
    • Mar 2007
    • 679

    #31
    nazwa is a string and yes i used equals(nazwa.eq uals(k.nazwa)) in the equals method

    Comment

    • oll3i
      Contributor
      • Mar 2007
      • 679

      #32
      but now what to do with the buttons problem

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #33
        Originally posted by oll3i
        but now what to do with the buttons problem
        I don't know; for starters: are both ActionListeners registered with those Buttons
        using the same List? Add a lot of System.out.prin tln(...) debug statements and
        see what happens.

        kind regards,

        Jos

        ps. does your equals() and hashCode() methods work correctly now?
        Last edited by JosAH; Apr 7 '07, 03:21 PM. Reason: added the ps.

        Comment

        • oll3i
          Contributor
          • Mar 2007
          • 679

          #34
          thank u a lot lot lot yes they do :)

          Comment

          • oll3i
            Contributor
            • Mar 2007
            • 679

            #35
            i did use lots of System.out.prin t() but the prob is that what's created by one button is not seen by the other button code and where to look 4 solution of that?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #36
              Originally posted by oll3i
              i did use lots of System.out.prin t() but the prob is that what's created by one button is not seen by the other button code and where to look 4 solution of that?
              Are both ActionListeners using the same List in which you added your Klient
              objects? Print out both lists everytime you access them and see what's in
              them. (they both should be two references to one List).

              kind regards,

              Jos

              ps. a List can print itself quite nicely because it overrides the toString() method.

              Comment

              • oll3i
                Contributor
                • Mar 2007
                • 679

                #37
                i walked through the list and i know that the client is in that list but when i do
                int index = klienci.indexOf (klient); it returns false

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #38
                  Originally posted by oll3i
                  i walked through the list and i know that the client is in that list but when i do
                  int index = klienci.indexOf (klient); it returns false
                  Can't be, the indexOf() method returns an int; -1 possibly if no object was found.
                  I think you have to recheck your equals() method and your hashCode() method.
                  Print out what your equals() method has to say about every comparison.

                  kind regards,

                  Jos

                  Comment

                  • oll3i
                    Contributor
                    • Mar 2007
                    • 679

                    #39
                    that was with contains sorry so it returns -1

                    but
                    Klient k1 = new Klient("Jan",20 0);
                    Klient k2 = new Klient ("Jan",200);

                    System.out.prin t(k1.equals(k2) );


                    returns true


                    int index = klienci.indexOf (klient);

                    if(index != -1) {
                    klient = klienci.get(ind ex);
                    }
                    else {
                    // 'klient' is not in the list 'klienci'
                    }

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #40
                      Originally posted by oll3i
                      that was with contains sorry so it returns -1

                      but
                      Klient k1 = new Klient("Jan",20 0);
                      Klient k2 = new Klient ("Jan",200);

                      System.out.prin t(k1.equals(k2) );


                      returns true


                      int index = klienci.indexOf (klient);

                      if(index != -1) {
                      klient = klienci.get(ind ex);
                      }
                      else {
                      // 'klient' is not in the list 'klienci'
                      }
                      You did do a 'klienci.add(k1 )' or 'klienci.add(k2 )' did you? If so, can you show
                      your equals method just to be sure? Add a System.out.prin tln() to your equals()
                      method to see if it is called by your List.

                      kind regards,

                      Jos

                      Comment

                      • oll3i
                        Contributor
                        • Mar 2007
                        • 679

                        #41
                        public boolean equals(Object o) {
                        if (!(o instanceof Klient)) return false;
                        Klient k = (Klient) o;
                        return nazwa.equals(k. nazwa) && pieniadze == k.pieniadze;
                        }

                        i will show u how it is with the buttons

                        Code:
                        m_add_client.addActionListener( new ActionListener() {
                                            public void actionPerformed(ActionEvent e) {
                                            	
                        try{
                                            		
                         //Klient k = new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()); 
                        
                        
                        klienci.add(new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()));               		
                                            	
                        }catch(NumberFormatException exception){
                        System.err.println("Nie podana suma pieniedzy");
                        }  
                        catch(NullPointerException exception){
                        System.err.println("?");
                        }
                        }
                        });

                        then the other button

                        Code:
                         m_add.addActionListener( new ActionListener() {
                                            public void actionPerformed(ActionEvent e) {
                                            	
                                            	try{
                                            	Cennik c= Cennik.getInstance();
                                            	Klient klient = new Klient((String)m_name.getText(),Double.valueOf(m_money.getText().trim()).doubleValue()); 
                                            	
                                            	
                                            
                                            	
                        
                        
                                            
                        //if(k.equals(klient)) {klient=k;}  // k is not seen here but that wd solve my problem  i wd only compare the two client objects 
                        
                        //but i have to find a client in a list 
                                               int index = klienci.indexOf(klient);        
                        
                                            	if(index != -1) {
                                            	  klient = klienci.get(index);
                                            	  
                                            	} 
                                            	else {
                                            	  // 'klient' is not in the list 'klienci'
                                            	}
                        
                                              and the rest of the code

                        Comment

                        • oll3i
                          Contributor
                          • Mar 2007
                          • 679

                          #42
                          BTW Happy Easter

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #43
                            Originally posted by oll3i
                            BTW Happy Easter
                            Happy Easter to you too and to everybody else reading this.

                            About your equals() method: it seems ok (as well as the rest of your code)
                            except for one thing: you're comparing two double values; two double values
                            are almost never equal to each other. Can you add some System.out.prin tln()
                            methods in your equals() method showing exactly *what* is compared to *what*.
                            Also print out what the equals() method wants to return. To be sure remove those
                            doubles from your hashCode() method, i.e. return the hashCode() if those Strings
                            only.

                            I think you're almost there. ;-)

                            kind regards,

                            Jos
                            Last edited by JosAH; Apr 7 '07, 06:22 PM. Reason: corrected a typo.

                            Comment

                            • oll3i
                              Contributor
                              • Mar 2007
                              • 679

                              #44
                              but first i have the equals method but where in the code is place for that method if i already have a list and i can get the object with klient = klienci.get(ind ex);

                              also two doubles that may be the reason why int index = klienci.indexOf (klient); returns -1

                              Comment

                              • JosAH
                                Recognized Expert MVP
                                • Mar 2007
                                • 11453

                                #45
                                Originally posted by oll3i
                                but first i have the equals method but where in the code is place for that method if i already have a list and i can get the object with klient = klienci.get(ind ex);

                                also two doubles that may be the reason why int index = klienci.indexOf (klient); returns -1
                                Good morning; you could (and should) put these System.out.prin tln() statements
                                in your own equals() method, i.e. print the values of the two objects to be
                                compared as well as the boolean that is to be returned to the caller. The List
                                is the caller of course and you can't change anything in the List code itself.

                                kind regards,

                                Jos

                                Comment

                                Working...