How to check and insert data before adding and storing in an array in Java?

Good day .. I have an address book program ... it works correctly, but does not check if the user input has already been saved in the array .. I want it to be like ... after receiving the user, the input compares it with the saved record in the array and when it is unique ... it will allow the user to add a new entry ...

here is my old code in addEntry ():

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++;

}

and here is what I plan to do, but it turns out to be ERROR:

public void addEntry () {

    entry[counter] = new AddressBookEntry();
    SName = JOptionPane.showInputDialog("Enter name: ");//<-- asks user for the name
    if (!entry[counter].getName().equals(SName)) {//<--compare
        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++;
    }
}

This is an error: An exception in the "main" thread java.lang.NullPointerException in AddressBook.addEntry (AddressBook.java:57) in AddressBook.main (AddressBook.java:28) Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)

, ... ... ,

, ,

:

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class AddressBook {

    private AddressBookEntry entry[];
    private int counter;
    private String SName;
    private int notfound = 0;

    public static void main(String[] args) {
        AddressBook a = new AddressBook();
        a.entry = new AddressBookEntry[100];
        int option = 0;
        try {
            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!");
                }
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Please Choose a Number in the displayed Menu");
        }
    }

    public void addEntry() {
        entry[counter] = new AddressBookEntry();
        SName = JOptionPane.showInputDialog("Enter name: ");
        if (entry[counter] == null &&  entry[counter].getName() == null
                && !entry[counter].getName().equals(SName)) {
            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 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;
        for (int i = 0; i < 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 to find: ");
        searchMethod();
    }

    public void searchMethod() {
        for (int i = 0; i < 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() {
        SName = JOptionPane.showInputDialog("Enter Name to edit: ");
        for (int i = 0; i < 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 to delete: ");
        if (SName == null) {
            return;
        }
        for (int i = 0; i < counter; i++) {
            if (entry[i] != null && SName.equals(entry[i].getName())) {
                entry[i] = null;
                JOptionPane.showMessageDialog(null, "Found!");
                break;
            }
        }
    }
}

, ... , . ... , ...

+3
6

, null, getName().

if (!entry[counter].getName().equals(SName))

:

if  (entry[counter] != null 
 &&  entry[counter].getName() != null 
 && !entry[counter].getName().equals(SName))
+2

Refactor java.util.Map, , AddressBookEntry - . , Map.containsKey(SName). .

, , , . Thats = 0... < .

0

AddressBook.addEntry, , , , . , - , .

0
entry[counter] = new AddressBookEntry();

, getName, , ( , setName ..).

: , , . ? . , .

0

, AddressBookEntry getName(). , entry[counter].getName() . , .equals() NullPointerException.

:

  • .equals()? AddressBookEntry , . , .
  • Perform a zero check first for each response to the glowcoder signal.
0
source

It is common practice to create a reference to the object and then assign its position in the array, so instead:

entry[counter] = new AddressBookEntry();
entry[counter].setXxx();
...

Preferably:

AddressBookEntry newEntry = new AddressBookEntry();
newEntry.setXxx();
...
entry[counter] = newEntry;

That way you can iterate through the array and determine if the record has already been inserted and not insert it again.

0
source

Source: https://habr.com/ru/post/1793198/


All Articles