java - Problem of Logic in Addressbook, should have no blank entries or same entry... Please Help -
hope can me coding.
what need do: 1.) should not allow user have blank entry.. should prompt them "no name inputted!" 2.) should not allow user input same entry..
i have no idea how code i, please help.
here's complete code:
public class addressbook {
private addressbookentry entry[]; private int counter; private string sname; private int notfound=0; public static void main(string[] args) { addressbook = new addressbook(); a.entry = new addressbookentry[100]; int option = 0; while (option != 5) { string content = "choose option\n\n" + "[1] add entry\n" + "[2] delete entry\n" + "[3] update entry\n" + "[4] view entries\n" + "[5] view specific entry\n" + "[6] exit"; option = integer.parseint(joptionpane.showinputdialog(content)); switch (option) { case 1: a.addentry(); break; case 2: a.deleteentry(); break; case 3: a.editentry(); break; case 4: a.viewall(); break; case 5: a.searchentry(); break; case 6: system.exit(1); break; default: joptionpane.showmessagedialog(null, "invalid choice!"); } } } public void addentry() { entry[counter] = new addressbookentry(); entry[counter].setname(joptionpane.showinputdialog("enter name: ")); entry[counter].setadd(joptionpane.showinputdialog("enter add: ")); entry[counter].setphoneno(joptionpane.showinputdialog("enter phone no.: ")); entry[counter].setemail(joptionpane.showinputdialog("enter e-mail: ")); counter++; } public void viewall() { string addtext = " name\taddress\tphone no.\te-mail add\n\n"; int nonnull = 0; (int = 0; < entry.length; i++) { if (entry[i] != null) { addtext = addtext + entry[i].getinfo() + "\n"; nonnull++; } if (nonnull == counter) { break; } } joptionpane.showmessagedialog(null, new jtextarea(addtext)); } public void searchentry() { sname = joptionpane.showinputdialog("enter name find: "); searchmethod(); } public void searchmethod() { (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { joptionpane.showmessagedialog(null, entry[i].getinfo2()); notfound = 0; break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } } public void editentry() { int notfound = 0; sname = joptionpane.showinputdialog("enter name edit: "); (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { entry[i] = new addressbookentry(); entry[i].setname(joptionpane.showinputdialog("enter new name: ")); entry[i].setadd(joptionpane.showinputdialog("enter new add: ")); entry[i].setphoneno(joptionpane.showinputdialog("enter new phone no.: ")); entry[i].setemail(joptionpane.showinputdialog("enter new e-mail: ")); notfound = 0; break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } } public void deleteentry() { sname = joptionpane.showinputdialog("enter name delete: "); (int = 0; < counter; i++) { if (entry[i].getname().equals(sname)) { joptionpane.showmessagedialog(null, "found!"); entry[i] = null; break; } } }
}
what happen code allows user click "ok" doesn't enter character yet.. , allows identical entry...
hope can me... need , yet still have no idea add in code... please lot..
what happen code allows user click "ok" doesn't enter character yet.. , allows identical entry...
the showinputdialog
method lets user input string wants, or nothing. if don't want this, can either use self-made dialog, patch joptionpane (using subclass or such), or show dialog again when not 1 of given strings, this:
string input = joptionpane.showinputdialog(content); try { option = integer.parseint(input); } catch(numberformatexception ex) { // user typed nothing, or not integer // todo: show error message // => go next iteration of while loop. continue; } // , our switch ...
Comments
Post a Comment