Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3976486
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:48:38+00:00 2026-05-20T04:48:38+00:00

What Exception do I need to add to my try catch block if I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T04:48:38+00:00Added an answer on May 20, 2026 at 4:48 am

    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”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please figure this out. The code runs properly without any exception. try { FTPClient
Code: catch (IOException e) { LOGGER.error(IOException exception happened); //now need throw again the same
Do I need to add any packages for the below code to execute successfully?
I need raise an exception from C++ code. Raised exception must contain text and
I need to catch that exception but I can't figure out which one it
I really want to get this out of my head. Please see below code:
Given the following code, i see no more need for the finally block for
This is the situation. I need to add PDF generation to a program that
So there is this website that has a login form. I want to log
This is a followup question to this Should I stick with the Try/Catch/Finally construct,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.