swing - Problem with my conditions in Java -
good day...
i have problem in searching entries in addressbook program... search them... i'm having problem showing "name not found!" message if name not yet saved in addressbook. can me figure out what's wrong this...
public void searchentry() { int notfound = 0; sname = joptionpane.showinputdialog("enter name find: "); (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { joptionpane.showmessagedialog(null, entry[i].getinfo2()); }else{ notfound++; } if (notfound != 0){ joptionpane.showmessagedialog(null, "name not found!"); } } }
can me show "name not found" msg... thnx in advance
move this:
if (notfound != 0){ joptionpane.showmessagedialog(null, "name not found!"); }
outside loop.
i realized you're going have shown every single one, because you're triggering if it's not found once. try this:
sname = joptionpane.showinputdialog("enter name find: "); boolean found = false; object info; (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { found = true; info = entry[i].getinfo2(); break; } } if (found){ joptionpane.showmessagedialog(null, info); }else{ joptionpane.showmessagedialog(null, "name not found!"); }
Comments
Post a Comment