java - finding element -
i´ve got method:
public user findbyid(string name) { (user u : list) { if (u.getfirstname() == name) { return u; } } return null; // or empty user }
how can program method in window class can print out user i´ve found?
this got. (think im way off)
public void findcustomer() { string firstname = findcustomerfield.gettext(); if(!firstname.equals("")) { string result = list.findbyid(firstname); outputtext.settext(result); } }
don't use ==
in comparison
if (u.getfirstname() == name)
but equal
if (u.getfirstname().equal(name))
the operator ==
tests see if 2 object reference variables refer exact same instance of object.
the method .equals()
tests see if 2 objects being compared each other equivalent
you need change findcustomer method this
public void findcustomer() { string firstname = findcustomerfield.gettext(); if(!"".equal(firstname)) { user user = list.findbyid(firstname); if(user != null) { outputtext.settext(user.getfirstname()); } } }
Comments
Post a Comment