My Main java class JOptionPane is not working.

My Mainjava class JOptionPane is not working.

import javax.swing.JOptionPane;

import java.util.*;

public class Main {

public static void main(String[] args){

  String dash= ("----------------------------------");

  Scanner input= new Scanner(System.in);

     Date date = new Date();



        System.out.println(date);

        JOptionPane.showInputDialog(null,

                "Welcome to the Hotel \n " +dash  +

                        " \n 1. Add a Visitor \n "     +

                            "2. View All Visitors \n " +

                            "3. Delete a Visitor \n "     +

                            "Choose Option Number: "   );

        int menu= input.nextInt();

        if(menu==1)

        {

        Info in= new Info();

        in.display();

        }

        if(menu==2)

        {

           JOptionPane.showMessageDialog(null, "Test");

           //create a new class called view and call the array list and print it



        }

        if(menu==3)

        {

           Delete del= new Delete();

           del.display();

        }

        else if(menu!=1 | menu!=2 | menu !=3)

        {

           JOptionPane.showMessageDialog(null, "Doesnt exist");

        }                             //prints out regardless of number

     }

}

The Correct Answer and Explanation is :

Your Main.java class is designed to display a menu using JOptionPane and handle user input through the Scanner class. However, there are several issues that need to be addressed to ensure the program functions correctly:

  1. User Input Handling: You’re using JOptionPane.showInputDialog to display the menu and prompt the user for input, but then you’re using Scanner to read the input. This approach is inconsistent. Since JOptionPane.showInputDialog returns the user’s input as a String, you should capture this input directly and convert it to an integer for menu selection.
  2. Logical Flow and Error Handling: The current structure of your if statements can lead to unintended behavior. Specifically, the else if condition else if(menu!=1 | menu!=2 | menu !=3) will always evaluate to true because a number cannot be simultaneously equal to 1, 2, and 3. This means that any input, including valid ones, will trigger the “Doesn’t exist” message.
  3. Class Definitions: The classes Info and Delete are referenced but not defined in the provided code. Ensure these classes are properly implemented elsewhere in your project.

Here’s a revised version of your Main.java class addressing these issues:

import javax.swing.JOptionPane;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        String dash = "----------------------------------";
        Date date = new Date();

        // Display the current date
        System.out.println(date);

        // Display the menu and get user input as a String
        String input = JOptionPane.showInputDialog(null,
                "Welcome to the Hotel \n" + dash +
                        "\n 1. Add a Visitor \n" +
                        "2. View All Visitors \n" +
                        "3. Delete a Visitor \n" +
                        "Choose Option Number:");

        try {
            // Convert the input to an integer
            int menu = Integer.parseInt(input);

            // Handle menu selection
            switch (menu) {
                case 1:
                    Info in = new Info();
                    in.display();
                    break;
                case 2:
                    JOptionPane.showMessageDialog(null, "Test");
                    // Implement the logic to view all visitors
                    break;
                case 3:
                    Delete del = new Delete();
                    del.display();
                    break;
                default:
                    JOptionPane.showMessageDialog(null, "Option doesn't exist");
                    break;
            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Invalid input. Please enter a number.");
        }
    }
}

Explanation:

  • Consistent User Input Handling: The program now uses JOptionPane.showInputDialog to capture user input as a String and then attempts to convert this input to an integer using Integer.parseInt(input). This approach eliminates the need for the Scanner class and ensures consistency in how user input is handled.
  • Error Handling: A try-catch block is used to catch NumberFormatException, which occurs if the user enters non-numeric input. If such an exception is caught, an error message is displayed to the user, prompting them to enter a valid number.
  • Logical Flow: The switch statement replaces the series of if statements, providing a clearer and more efficient way to handle menu selections. The default case handles any invalid menu options, displaying an appropriate message to the user.
  • Class Definitions: Ensure that the Info and Delete classes are properly defined in your project. The display methods in these classes should contain the logic for adding and deleting visitors, respectively.

By implementing these changes, your program will handle user input more robustly and provide clearer feedback to the user, enhancing the overall user experience.

Scroll to Top