Hi, I was asked to make a Phone Book thats saves entries and call them up. Then the teacher added a new task. The Program should delete entries. He gave us a methode and said we are not allowed to use ArrayList or Hashmaps, ONLY Arrays. I tried alot but just could not seem to get it right, Can someone please help me?

import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

public class PhoneBook {

    private static int maxSize = 100; 
    private static int actSize = 0; 
    private static String[] name = new String[maxSize]; 
    private static String[] firstName = new String[maxSize]; 
    private static String[] number = new String[maxSize]; 

    public static String findEntry(String nameP, String firstNameP) {
        for (int i = 0; i < actSize; i++) {
            if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
                return number[i];
            }
        }
        return null;
    }

    public static void addEntry(String nameP, String firstNameP, String numberP) {
        if (actSize == maxSize) {
            System.err.println("Memory full!");
            return;
        }
        for (int i = 0; i < actSize; i++) {
            if (name[i].equals(nameP) && firstName[i].equals(firstNameP)) {
                System.err.println("It exists already!");
                return;
            }
        }
        name[actSize] = nameP;
        firstName[actSize] = firstNameP;
        number[actSize] = numberP;
        actSize = actSize + 1;
    }

    public static String getActionDialog(String... actions) { 

        JRadioButton[] buttons = new JRadioButton[actions.length];
        ButtonGroup group = new ButtonGroup();

        for (int i = 0; i<actions.length; i++) {
            buttons[i] = new JRadioButton(actions[i]);
            group.add(buttons[i]);
        }

    buttons[0].setSelected(true);
    Object[] message = buttons;
    Object[] options = { "OK", "Cancel"};


    int n= JOptionPane.showOptionDialog(null,
            message,
            "Choose an Action",
            JOptionPane.YES_NO_OPTION,
            JOptionPane.QUESTION_MESSAGE,
            null,
            options,
            options[0]);

    if(n==JOptionPane.OK_OPTION){
        for (int i = 0; i<actions.length; i++) {
            if(buttons[i].isSelected()){
                return actions[i];
            }
        }

    }

    return null;
    }

    /**

     * Deletes Entries.
     * An entry matches the pattern if it fits in all components (name, first name, number).
     * A component fits, when it is equal to the sample component, or the component pattern of the empty string.
     * 
     * @param nameP         Pattern for the name of the entries to be deleted.
     * @param firstNameP    Pattern for the first names of the entries to be deleted.
     * @param numberP       Pattern for the number of entries to be deleted.
     * @post  all matching entries are deleted. The capacity remains unchanged.
     *        The size is adjusted.
     * @return Number of deleted entries.
     */
    public static int deleteEntries(String nameP, String firstNameP, String numberP) {
        return 0;
    }

}






import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class UI {

    public static void main(String[] args) {
        while (true) {
            String action = PhoneBook.getActionDialog(
                    "Create an entry", "Call up an entry");
            if (action == null) {
                break;
            }
            if (action.equals("Create an entry")) {
                String[] entryData = getEntryDataDialog();
                if (entryData != null) {
                    PhoneBook.addEntry(entryData[0], entryData[1],
                            entryData[2]);
                }

            } else if (action.equals("Call up an entry")) {
                String[] queryData = getQueryDataDialog();
                if (queryData != null) {
                    String nr = PhoneBook.findEntry(queryData[0],
                            queryData[1]);
                    if (nr == null) {
                        JOptionPane.showMessageDialog(null, "Not found");
                    } else {
                        JOptionPane.showMessageDialog(null, "Last Name: "
                                + queryData[0] + ", First Name: " + queryData[1]
                                + ", Number:" + nr);
                    }
                }

            } else {
                System.err.println("Error: unknow Action " + action);
                break;
            }
        }
    }

    private static String[] getEntryDataDialog() {
        JTextField nameTF = new JTextField();
        JTextField firstNameTF = new JTextField();
        JTextField numberTF = new JTextField();

        Object[] message = { "Last Name", nameTF, "First Name", firstNameTF, "Number",
                numberTF };
        Object[] options = { "OK", "Cancel" };

        int n = JOptionPane.showOptionDialog(null, message, "Create an entry",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, options[0]);

        if (n == JOptionPane.OK_OPTION) { 
            return new String[] { nameTF.getText(), firstNameTF.getText(),
                    numberTF.getText() };

        } else if (n == JOptionPane.NO_OPTION
                || n == JOptionPane.CLOSED_OPTION) { 
            return null;
        } else {
            return null;
        }
    }

    private static String[] getQueryDataDialog() {
        JTextField nameTF = new JTextField();
        JTextField firstNameTF = new JTextField();

        Object[] message = { "Last Name", nameTF, "First Name", firstNameTF };
        Object[] options = { "OK", "Cancel" };

        int n = JOptionPane.showOptionDialog(null, message, "Call up an entry",
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                options, options[0]);

        if (n == JOptionPane.OK_OPTION) { 
            return new String[] { nameTF.getText(), firstNameTF.getText() };

        } else if (n == JOptionPane.NO_OPTION 
                || n == JOptionPane.CLOSED_OPTION) { 
            return null;
        } else {
            return null;

        }
    }


}

What did you try? All you have shown is return 0;
You just need a loop that goes through the array checking each entry to see if it matches the patterns, and deleting it if it matches.
You can't just "delete" an entry in an array like that - you have to move all the entries after it down one position, then update the size variable to match.
eg if you have an array with values
1, 2, 3, 4, 5, 0, 0, 0, 0, 0 size = 5
and you want to delete the "3" entry, then after deletion you should have
1, 2, 4, 5, 0, 0, 0, 0, 0, 0 size = 4

Go through the array using a for loop and then if name[i] == nameP then make name[i] = NULL;

^That will just make the value empty at that position of the array.

After this you need to shift the values towards right from that position to the left.

Hope that helps :)

Edited 2 Years Ago by kal_crazy

I did the following

public static int deleteMatchingEntries(String nameP, String firstNameP,
            String numberP) {

        for (int i = 0; i < actSize - 1; i++) {
            if (name[i] == nameP) {
                name[i] = name[i + 1];
            } else if (firstName[i] == firstNameP) {
                firstName[i] = firstName[i + 1];
            } else if (number[i] == numberP) {
                number[i] = number[i + 1];
            }
            actSize--;
        }

        return actSize;
    }

I saved 5 entries. When I try to call up the 3rd entry, the 4th an 5th entries will be deleted but the 3th will not.

Three things
1. You can't test your Strings with == (are exactly the same object). You have to use String's equals method (have the same sequence of characters in them)
2. If you delete entries from one array but not the others they will get out of step and your data will be garbled. You need to test all three, decide whether to delete, then delete from all three together.
3. It's not enough just to move one entry down, you need a loop moving all the following entries in the array.

That is because each time you go through the loop you are decrementing actSize.

Decrement once the loop has found the entry and deleted it.

Ok, I have this so far. When I delete the first name, the next first name will take place of the deleted first name and its the same for the rest.

public static int deleteMatchingEntries(String nameP, String firstNameP,
            String numberP) {
        for (int i = 0; i < actSize; i++) {
            if (name[i].equals(nameP)) {
                name[i] = name[i + 1];

                actSize--;
            } else if (firstName[i].equals(firstNameP)) {
                firstName[i] = firstName[i + 1];

                actSize--;
            } else if (number[i].equals(numberP)) {
                number[i] = number[i + 1];

                actSize--;
            }
        }
        return actSize;
    }

I posted it there, so I can have other opinios, is it not allowed?

this is info for potential answerers, to avoids wasting with their free time here

Edited 2 Years Ago by mKorbel

Ok, thanks alot guys, I will try to change it until it works.

This question has already been answered. Start a new discussion instead.