arrays - Logic of deleting in Java -
i cant figure out how start method delete specific entry stored in array...
i used this:
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; } } }
but advised not assign entry[i]
null because ruin entries...
i have no idea how code in way...
what should need is: need delete specific entry array please help...
also... output error says:
exception in thread "main" java.lang.nullpointerexception
@ addressbook.viewall(addressbook.java:62)
@ addressbook.main(addressbook.java:36)
java result: 1
this code in main program:
public class addressbook { private addressbookentry entry[]; private int counter; private string sname; 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 = 0; < counter; i++) { addtext = addtext + entry[i].getinfo() + "\n"; } joptionpane.showmessagedialog(null, new jtextarea(addtext)); } 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()); break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } notfound = 0; } 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: ")); break; } else { notfound++; } } if (notfound != 0) { joptionpane.showmessagedialog(null, "name not found!"); } notfound = 0; } 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; } } } }
assigning values null going easiest practice. if you're picky, resize array, rather pointless. keep separate size counter , decrement each time set null.
another reason you're getting null pointer exception have consider what's happening when you're replacing values in array null still iterating counter. you're going left holes in array upon deletion. first solution bypass null values altogether, , shift array down (somewhat of expensive operation). second alter methods take null values consideration. example:
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));
}
i don't have compiler on computer, consider more of psuedo-code. idea counter keeping track of how many non-null values have in address book, , these null values in random places of array. added nonnull integer local counter keep track of how many values you've encountered aren't null (so aren't forced run through entire address book). then, added if statement ensure value @ entry[i] isn't null value (trying invoke getinfo() on null value what's giving error). lastly, added if statement break loop if you've encountered of non-null values have stored. hope helps. (also may worth considering linkedlist eliminate null values together).
actually, simplicity's sake, better off using linkedlist, unless required use array, since need alter of methods take null spaces in array account. assuming you're familiar linkedlists of course.
Comments
Post a Comment