Results 1 to 12 of 12
Thread: Need help with Phonebook program
- 12-04-2012, 04:23 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Need help with Phonebook program
I have an assignment for Intro to Java class in which a phonebook directory is used. When the program runs, the user must select File<Open then select the phonebook file that is of .dat type. Then a phonebook directory opens up with compiled names and numbers. The user has a few options such as entering a new entry, updating the list with a new entry, deleting an entry, searching for a name, or sorting them.
My task is to modify the program providing the implementation for:
-deleting an entry
-searching for an entry
-sorting the entries alphabetically
I'm not sure what's wrong with my delete function. Also, how do I perform the search and sort functions? I think for the sort function I need to assign the entries to integer values to allow them to be compared, but I am unsure how to.
Here is my code so far:
Java Code: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 As ...")); 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 and 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 a new entry nameList.add(name); numberList.add(number); nameList.select(nameList.getItemCount()-1); } else { // update an existing entry nameList.replaceItem(name, index); numberList.replaceItem(number, index); nameList.select(index); } } else if(source == newEntryMI) { nameList.select(-1); display(-1); } else if(source == searchMI) { String searchName = JOptionPane.showInputDialog(this, "Please enter a name (last first) to search:"); System.out.println("Name to search: " + searchName); } else if(source == deleteMI){ int index = nameList.getSelectedIndex(); String name = lastName.getText() + " " + firstName.getText(); String number = phoneNumber.getText(); if(index == 0) { // delete an entry nameList.remove(name); numberList.remove(name); /* int index= nameList.getSelectedIndex(); String name = nameList.getItem(index); String number = phoneNumber.getText(); if(index < 0){ nameList.remove(name); numberList.remove(number); nameList.select(nameList.getItemCount()-1); numberList.select(numberList.getItemCount()-1); } else { /* int space = name.indexOf((int) ' '); lastName.setText(name.substring(0, space)); firstName.setText(name.substring(space+1)); phoneNumber.setText(numberList.getItem(index)); */ } } else if(source == sortMI){ } } /** * Implementing ItemListener to display the selected entry */ public void itemStateChanged(ItemEvent event) { display(nameList.getSelectedIndex()); } /** * method to specify and open a file */ private void doOpen() { // display file selection dialog FileDialog fDialog = new FileDialog(this, "Open ...", FileDialog.LOAD); fDialog.setVisible(true); // Get the file name chosen by the user String file = fDialog.getFile(); // If user canceled file selection, return without doing anything. if(file == null) return; fileName = fDialog.getDirectory() + file; // Try to create a file reader from the 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 from before if any nameList.removeAll(); numberList.removeAll(); // Try to read from the input file one line at a 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 to display the 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)); } } private void doDelete(){ } private void selectionSort(int[] aList) { int temp; int index; for(int i=0; i<aList.length-1; i++) { index = i; for(int j=i+1; j<aList.length; j++) { if(aList[j] < aList[index]) index = j; } temp = aList[index]; aList[index] = aList[i]; aList[i] = temp; } } /** * the 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)"); // add window closing listener frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); // show the frame frame.setVisible(true); } }Last edited by evvdogg; 12-04-2012 at 04:41 PM.
- 12-04-2012, 04:34 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: Need help with Phonebook program
If you add some code tags around the code above, it would help.
I would suggest making a separate class that holds a "Phone Book Entry" information (Name/Number) and removing the Lists, maybe even breaking the name up into parts, but this is not technically necessary. Similar to how you have the "display" method set up, you could create a "delete" method, that removes the selected index from the information lists. You would just have to be careful and make sure you remove it from all the Lists. The "Sort" function would be a little trickier with the information stored the way it is, as any changes made to the "Name" list would have to be made to the "Number" list.
- 12-04-2012, 04:43 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Re: Need help with Phonebook program
My bad I re-posted it with the code tags so it should be easier to read now.
- 12-04-2012, 04:53 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: Need help with Phonebook program
Look at your check on the index in the "delete" handling... What if it isn't the 1st entry?
Is the "Number" List even being displayed? All this keeping of the data in the GUI is driving me crazy...
- 12-04-2012, 05:21 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Re: Need help with Phonebook program
Would it be better then if I did if (index >=0) then an else? I'm not sure what to put in the else though.
- 12-04-2012, 05:30 PM #6
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
- 12-04-2012, 05:52 PM #7
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
- 12-04-2012, 06:40 PM #8
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: Need help with Phonebook program
The APIs can be your friend.
List (Java Platform SE 7 )
Take a look at the documentation, I'm sure you can find a method that would work for what you are doing. It might take specifying the index to be removed.
- 12-05-2012, 04:20 AM #9
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Re: Need help with Phonebook program
Okay, I figured out how to get the delete function to work. I also want it to select and show the next entry in the list. Any ideas how to do that?
This is this my code now for this part of the program:
Java Code:else if(source == deleteMI){ int index = nameList.getSelectedIndex(); String name = lastName.getText() + " " + firstName.getText(); String number = phoneNumber.getText(); if(index >=0){ nameList.remove(index); numberList.remove(index); nameList.select(index-1); //this doesn't do anything. numberList.select(index-1); //doesn't do anything. } }
- 12-05-2012, 03:51 PM #10
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: Need help with Phonebook program
Have you tried debugging through the code or adding some System.out.println() statements to see what the "index" is and after the "nameList.select(index-1)" what nameList's selected index is?
I would avoid setting the selected index to index-1, what would happen if the first item (index = 0) is deleted?
- 12-06-2012, 02:42 AM #11
Member
- Join Date
- Dec 2012
- Posts
- 1
- Rep Power
- 0
Re: Need help with Phonebook program
After finding this I'm wondering what sorting method would be good to use in this and how? I'm new to learning Java and have been wanting to learn some methods to sorting, so what would be good to use here? I've only ever used bubbleSort for integers but I've never sorted a List before, so any help would be appreciated!
- 12-07-2012, 04:05 AM #12
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Re: Need help with Phonebook program
Can someone help me with the sort? I'm really not sure how to do it with this program.
This is what I have and all the code I've attempted to try. Most of it has been commented out:
Java Code:else if(source == sortMI){ /* ArrayList name= new ArrayList(); ArrayList number= new ArrayList(); int size1= SizeList(); for int */ // selectionSort(int[] nameList); // String lastName1= lastName1.getText().trim(); // String lastName2= lastName2.getText().trim(); // (lastName1).compareTo(lastName2); // String Name = lastName.getText().trim() + " " + firstName.getText().trim(); // ArrayList<String> names = new ArrayList<String>(50); // Collections.sort(nameList<List>); } // List<String>sortedlist=Collections.sort(nameList); { // } // String Entry1 = nameList.LastName // String Entry2= nameList.LastName /* int result = Entry1.compareTo(Entry2); if (result< 0){ System.out.printf(Entry1, Entry2); } else if(result>0){ System.out.printf(Entry2, Entry1); } */ // inOrder(Entry1, Entry2); }
Similar Threads
-
Workout program progression - Ways to make my Java program shorter/smarter?
By LasseA in forum New To JavaReplies: 4Last Post: 11-21-2012, 01:19 PM -
Help! Phonebook.java Project
By javahelp1234 in forum New To JavaReplies: 1Last Post: 03-30-2012, 01:43 AM -
Phonebook HashMap Example
By forms in forum New To JavaReplies: 3Last Post: 01-23-2012, 07:09 PM -
Java Phonebook With Arrays // HELP! //
By K-Scale in forum New To JavaReplies: 9Last Post: 10-04-2011, 03:41 AM -
phonebook update
By nanna in forum New To JavaReplies: 5Last Post: 03-09-2009, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks