0

hope you can help me coding.

What i need to do: 1.) I should not allow user to have a blank entry.. I should prompt them like "No name inputted!" 2.) I should not allow user to input same entry..

I have no Idea how to code i, please help.

Here's my complete code:

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;
    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";
    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() {
    int notfound = 0;
    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: ");
    for (int i = 0; i < counter; i++) {
        if (entry[i].getName().equals(SName)) {
            JOptionPane.showMessageDialog(null, "Found!");
            entry[i] = null;
            break;
        }
    }
}

}

What happen to my code was it allows user to click "OK" even he doesn't enter any character yet.. and it also allows identical entry...

Hope you can help me... i need it very soon and yet i still have no Idea what to add in my code... please help thanks a lot..

iamanapprentice
  • 411
  • 5
  • 19
  • 37
  • If you are using JOptionPane anyway, you should look at `showOptionDialog` which allows you showing a list of buttons instead of an input line. – Paŭlo Ebermann Feb 19 '11 at 02:43
  • @Sir Paulo... good day... i am new at java and not familiar with the built-in functions... i'll try using this thanks :) – iamanapprentice Feb 19 '11 at 02:46
  • @Sir Paulo.. think this is what i really need... but when i try to apply it java says:"it cannot be applied to given types".. This is the what i do: entry[counter].setName(JOptionPane.showOptionDialog("Enter name: ")); Please Help thanks – iamanapprentice Feb 19 '11 at 02:49
  • @Sir Paulo... i've search about JOptionPane.showOptionDialog... but it doesnt asks for user input... what i need is... to check if the user inputed an entry or not.. and prompt him if he doesnt inputed any character yet... thanks in advance – iamanapprentice Feb 19 '11 at 02:53
  • Yes, showOptionDialog does not prompt for typing in input, but offers a list of options. I thought you wanted this. – Paŭlo Ebermann Feb 19 '11 at 13:43

1 Answers1

2

What happen to my code was it allows user to click "OK" even he doesn't enter any character yet.. and it also allows identical entry...

The showInputDialog method lets the user input any string he wants, or also nothing. If you don't want this, you can either use a self-made dialog, patch the JOptionPane (using a subclass or such), or simply show the dialog again when it is not one of your given strings, like this:

String input = JOptionPane.showInputDialog(content);
try {
   option = Integer.parseInt(input);
}
catch(NumberFormatException ex) {
   // the user typed nothing, or something that is not an integer
   // TODO: show error message
   // => go to next iteration of while loop.
   continue; 
}
// and now our switch ...
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
  • sorry... but can you help me code about knowing if the user typed nothing... i still have no idea... please help thanks – iamanapprentice Feb 19 '11 at 02:59
  • With my example above, the `parseInt` method throws an exception for "empty string", since this is not a valid integer. If you want to check on empty string, do a `if(input.equals("")) { ... }` test before this. (Or `input.isEmpty()` with Java 6 or later.) – Paŭlo Ebermann Feb 19 '11 at 03:05
  • O yes... but how about in my addEntry() Method... i want to detect if the user didn't input a name... sorry... i'm still a bit confused :( – iamanapprentice Feb 19 '11 at 03:13
  • It works the same way - put the result of the dialog to a variable and check its `isEmpty()` before calling `setName()`. Alternatively you could change your `setName` method to throw an exception when the name is empty (and would have to catch this exception). – Paŭlo Ebermann Feb 19 '11 at 03:16