开发者

is there any Exception in Java that to detect no input?

What Exception do I need to add to my try catch block if I want to detect if a user has entered any characters?

This is my code where I want to know if the user hasn't input anything or if the user's input was already saved in an addressbook. I am using an array to store my entries:

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

Do I need a try-catch or a condition? 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;
    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();
    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: ");
    for (int i = 0; i < counter; i++) {
        if (entry[i].getName().equals(SName)) {
            JOptionPane.showMessageDialog(null, "Found!");
            entry[i] = null;
            break;
        }
    }
}

}

I am having problem with my addEntry() method, because I want to detect if the user's new added entry was already stored in my addressbook and if the user doesn't typed anything when I ask "Enter Name:" in JOptionPane and still press OK.


Although not much clear about your question, I will try to answer it.
In this case, you can manually create your own exception here like,

throw new MyException();    

Since your condition "The user didn't enter any character" is itself not clearer, I would suggest that you can try throwing exception by checking the no of arguments passed on the command-line.
If it's 0 i.e no arguments are passed, you can throw your own exception.

But by itself, no exception will be thrown.


I don't know of any exception, but you can use the length method.

Example:

input=(JOptionPane.showInputDialog(frame1, "what do you want?"));
    if(input.length() > 0) {
        l1.setText("a " + input + " on the way!");
    }
    else{
        l1.setText("would you please choose something");
    }

if length is greater then zero


From your updated question, it appears as if you are talking about user input from dialog boxes. The javadoc says that the showInputDialog methods return either the user's String (which could be empty) or null if the user canceled the dialog.

No exception is thrown. The user canceling the dialog is not "exceptional".


In general, when some method throws an exception and you don't catch it, either the compiler alerts you about this (for checked exceptions) or when the exceptions is thrown on runtime, you see a nice stack trace on the console (standard error). If you don't see this, then either you have caught (and thrown away) this exception, or there is no exception.

The user didn't enter any character by itself is not a condition which causes anyone to throw an exception, we would need more context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜