nazwa is a string and yes i used equals(nazwa.eq uals(k.nazwa)) in the equals method
how to check if an object already exists
Collapse
X
-
I don't know; for starters: are both ActionListeners registered with those ButtonsOriginally posted by oll3ibut now what to do with the buttons problem
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?Comment
-
Are both ActionListeners using the same List in which you added your KlientOriginally posted by oll3ii 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?
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
-
Can't be, the indexOf() method returns an int; -1 possibly if no object was found.Originally posted by oll3ii 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
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,
JosComment
-
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
-
You did do a 'klienci.add(k1 )' or 'klienci.add(k2 )' did you? If so, can you showOriginally posted by oll3ithat 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'
}
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,
JosComment
-
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 codeComment
-
Happy Easter to you too and to everybody else reading this.Originally posted by oll3iBTW Happy Easter
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,
JosComment
-
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 -1Comment
-
Good morning; you could (and should) put these System.out.prin tln() statementsOriginally posted by oll3ibut 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
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,
JosComment
Comment