Note that there are some explanatory texts on larger screens.

plurals
  1. POLogic of deleting in Java
    primarykey
    data
    text
    <p>I cant figure out how to start a method to delete a specific entry stored in an array...</p> <p>I used to do this:</p> <pre><code>public void deleteEntry() { SName = JOptionPane.showInputDialog("Enter Name to delete: "); for (int i = 0; i &lt; counter; i++) { if (entry[i].getName().equals(SName)) { JOptionPane.showMessageDialog(null, "Found!"); entry[i] = null; } } } </code></pre> <p>but I was advised not to assign the <code>entry[i]</code> to null because it will ruin my entries...</p> <p>I have no idea how to code it in another way...</p> <p>What should I need to do is: I need to delete a specific entry from an array please help...</p> <p>also... its output was error it says:</p> <blockquote> <p>Exception in thread "main" java.lang.NullPointerException<br> at AddressBook.viewAll(AddressBook.java:62)<br> at AddressBook.main(AddressBook.java:36)<br> Java Result: 1</p> </blockquote> <p>This is my code in my main program:</p> <pre><code>public class AddressBook { private AddressBookEntry entry[]; private int counter; private String SName; public static void main(String[] args) { AddressBook a = new AddressBook(); a.entry = new AddressBookEntry[100]; int option = 0; while (option != 5) { String content = "Choose an Option\n\n" + "[1] Add an Entry\n" + "[2] Delete an Entry\n" + "[3] Update an Entry\n" + "[4] View all 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"; for (int i = 0; i &lt; 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 to find: "); for (int i = 0; i &lt; 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 to edit: "); for (int i = 0; i &lt; 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 to delete: "); for (int i = 0; i &lt; counter; i++) { if (entry[i].getName().equals(SName)) { JOptionPane.showMessageDialog(null, "Found!"); entry[i] = null; break; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload