java - Delete an Item from a List/Search for an Item in a list -
i'm working on lab have phonebook program. program opens box open .bat file containing list of names , numbers.
i can't figure out how deletemi function , searchmi function work.
any immensely appreciated
import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.joptionpane; public class phonebook extends frame implements actionlistener, itemlistener { menuitem newmi, openmi, savemi, saveasmi, exitmi; menuitem searchmi, deletemi, updatemi, newentrymi, sortmi; string filename; list namelist, numberlist; textfield lastname, firstname, phonenumber; /** * constructor */ public phonebook() { super("white pages"); // set frame title setlayout(new borderlayout()); // set layout // create menu bar menubar menubar = new menubar(); setmenubar(menubar); // create file menu menu filemenu = new menu("file"); menubar.add(filemenu); newmi = filemenu.add(new menuitem("new")); newmi.addactionlistener(this); openmi = filemenu.add(new menuitem("open")); openmi.addactionlistener(this); filemenu.addseparator(); savemi = filemenu.add(new menuitem("save")); saveasmi = filemenu.add(new menuitem("save ...")); filemenu.addseparator(); exitmi = filemenu.add(new menuitem("exit")); exitmi.addactionlistener(this); // create edit menu menu editmenu = new menu("edit"); menubar.add(editmenu); updatemi = editmenu.add(new menuitem("update")); updatemi.addactionlistener(this); newentrymi = editmenu.add(new menuitem("new entry")); newentrymi.addactionlistener(this); deletemi = editmenu.add(new menuitem("delete")); editmenu.addseparator(); searchmi = editmenu.add(new menuitem("search")); searchmi.addactionlistener(this); sortmi = editmenu.add(new menuitem("sort")); // create phone list , controls panel listpanel = new panel(new borderlayout()); add(listpanel, borderlayout.center); label label = new label("name list", label.left); listpanel.add(label, borderlayout.north); namelist = new list(); namelist.additemlistener(this); numberlist = new list(); listpanel.add(namelist, borderlayout.center); panel panel = new panel(new borderlayout()); add(panel, borderlayout.west); panel editpanel = new panel(new gridlayout(6, 1)); panel.add(editpanel, borderlayout.north); label = new label("last name", label.left); editpanel.add(label); lastname = new textfield(); editpanel.add(lastname); label = new label("first name", label.left); editpanel.add(label); firstname = new textfield(); editpanel.add(firstname); label = new label("phone number", label.left); editpanel.add(label); phonenumber = new textfield(); editpanel.add(phonenumber); } // implementing actionlistener public void actionperformed(actionevent event) { object source = event.getsource(); if(source == newmi) { namelist.removeall(); numberlist.removeall(); filename = null; display(-1); settitle("white pages"); // reset frame title } else if(source == openmi) { doopen(); } else if(source == exitmi) { system.exit(0); } else if(source == updatemi) { int index = namelist.getselectedindex(); string name = lastname.gettext().trim() + " " + firstname.gettext().trim(); string number = phonenumber.gettext().trim(); if(index < 0) { // add new entry namelist.add(name); numberlist.add(number); namelist.select(namelist.getitemcount()-1); } else { // update existing entry namelist.replaceitem(name, index); numberlist.replaceitem(number, index); namelist.select(index); } } else if(source == newentrymi) { namelist.select(-1); display(-1); } // can't delete work else if(source == deletemi) { int name = namelist.getselectedindex(); int number = numberlist.getselectedindex(); if(name >= 0){ namelist.remove(name); numberlist.remove(number); } } // can't search work else if(source == searchmi) { string searchname = joptionpane.showinputdialog(this, "please enter name (last first) search:"); system.out.println("name search: " + searchname); int index = namelist.getselectedindex(); string name = namelist.getitem(index); for(int i=0; < namelist.getitemcount(); i++){ if(name.equalsignorecase(searchname)){ namelist.select(index); system.out.println("we found him"); } } } else if(source == sortmi) { system.exit(0); } } /** * implementing itemlistener display selected entry */ public void itemstatechanged(itemevent event) { display(namelist.getselectedindex()); } /** * method specify , open file */ private void doopen() { // display file selection dialog filedialog fdialog = new filedialog(this, "open ...", filedialog.load); fdialog.setvisible(true); // file name chosen user string file = fdialog.getfile(); // if user canceled file selection, return without doing anything. if(file == null) return; filename = fdialog.getdirectory() + file; // try create file reader chosen file. filereader reader; try { reader = new filereader(filename); } catch (filenotfoundexception ex) { joptionpane.showmessagedialog(this, "file not found: " + filename, "error", joptionpane.error_message); doopen(); return; } bufferedreader breader = new bufferedreader(reader); // remove items before if namelist.removeall(); numberlist.removeall(); // try read input file 1 line @ time. try { int index; string name, number; string textline = breader.readline(); while (textline != null) { index = textline.indexof((int) ','); if(index > 0) { name = textline.substring(0, index); number = textline.substring(index+1); namelist.add(name.trim()); numberlist.add(number.trim()); } textline = breader.readline(); } breader.close(); reader.close(); } catch (ioexception ioe) { joptionpane.showmessagedialog(this, "error reading file: " + ioe.tostring(), "error", joptionpane.error_message); return; } settitle("white pages: " +file); // reset frame title namelist.select(0); display(0); } /** * method display current entry */ private void display(int index) { if(index < 0) { lastname.settext(""); firstname.settext(""); phonenumber.settext(""); } else { string name = namelist.getitem(index); int space = name.indexof((int) ' '); lastname.settext(name.substring(0, space)); firstname.settext(name.substring(space+1)); phonenumber.settext(numberlist.getitem(index)); } } /** * main method */ public static void main(string[] argv) { // create frame system.out.println("creating window ... "); phonebook frame = new phonebook(); dimension size = toolkit.getdefaulttoolkit().getscreensize(); frame.setsize(size.width/2, size.height/2); frame.setlocation(100, 100); system.out.println("your screen size: " + size.width + " (width) x " + size.height + " (height)"); //working on search functions string name1 = "john"; string name2 = "james"; if(name1.equalsignorecase(name2)){ system.out.println("the 2 names same"); } else system.out.println("the 2 names different"); //working on sort function string namea = "john"; string nameb = "james"; if(namea.comparetoignorecase(nameb) == 0) { system.out.println("the 2 names same"); } else if (namea.comparetoignorecase(nameb) < 0) system.out.println("name comes before name b"); else system.out.println("name comes after name b"); // add window closing listener frame.addwindowlistener(new windowadapter() { public void windowclosing(windowevent e) { system.exit(0); } }); // show frame frame.setvisible(true); } ![enter image description here][1]}
two things:
- you didn't add actionlistener deletemi
the searching loop incorrect, should be:
else if(source == searchmi) { string searchname = joptionpane.showinputdialog(this, "please enter name (last first) search:"); system.out.println("name search: " + searchname); string name; for(int i=0; < namelist.getitemcount(); i++){ name = namelist.getitem(i); if(name.equalsignorecase(searchname)){ namelist.select(i); //add code display data: string[] n = name.split(" "); firstname.settext(n[0]); lastname.settext(n[1]); phonenumber.settext(numberlist.getitem(i)); //end system.out.println("we found him"); } } }
but search works "last name + first name" string.
you used index variable in incorrect way. did't change loop.
the problem delete coused int number = numberlist.getselectedindex();
- don't have selected item (at least in code above, there no visible numberlist @ all), return -1. couse arrayoutofboundsexception. implement solution:
else if(source == deletemi) { int index = namelist.getselectedindex(); if(index >= 0){ namelist.remove(index); numberlist.remove(index); } }
as sort method, can now:
else if(source == sortmi){ string[] list = namelist.getitems(); string[] nums = numberlist.getitems(); arrays.sort(list); arrays.sort(nums); for(int = 0; i<list.length;i++){ namelist.replaceitem(list[i],i); numberlist.replaceitem(nums[i],i); } namelist.revalidate(); numberlist.revalidate(); }
it not elegant code, far know, works. hope suits needs.
Comments
Post a Comment