rlee
rlee

Reputation: 11

Joptionpane does not process switch-case

Am having issues with my switch-case options and joptionpane. After selecting one of the four options the program ends. It doesn't display the selection. It should display the choice/selection.

  import javax.swing.JOptionPane; // JOptionPane call

  public class OnlineStore 
  {
     // public static void main(String[] args) // main program
      public void display_menu() // Not the main program but the main menu.
      {
          String main_selection;
          int mSelect;

          main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

      }

      public OnlineStore() // Switch-case program
      {
          display_menu(); // Call display menu.
          switch (mSelect)
          {
              case 1:
                  JOptionPane.showMessageDialog(null, "Option 1");
                  break;
              case 2:
                  JOptionPane.showMessageDialog(null, "Option 2");
                  break;
              case 3:
                  JOptionPane.showMessageDialog(null, "Option 3");
                  break;
              case 4:  // Deliberately not including a default selection.
                  JOptionPane.showMessageDialog(null, "Option 4");
                  break;  
           } 

      } 

      public static void main(String[] args) // main program
      {
          new OnlineStore(); // Call out the program.
      }
  }

When I use Scanner the results are ok.

Upvotes: 0

Views: 1225

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347314

The main problem is that there is no contextual relationship between what is happening in display_menu and what your processing in OnlineStore.

Instead, why not have display_menu return the selected value/option and include that in the switch statement, for example

public class OnlineStore {

    // public static void main(String[] args) // main program

    public int display_menu() // Not the main program but the main menu.
    {
        String main_selection;

        main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
        return Integer.parseInt(main_selection);

    }

    public OnlineStore() // Switch-case program
    {
        switch (display_menu()) {
            case 1:
                JOptionPane.showMessageDialog(null, "Option 1");
                break;
            case 2:
                JOptionPane.showMessageDialog(null, "Option 2");
                break;
            case 3:
                JOptionPane.showMessageDialog(null, "Option 3");
                break;
            case 4:  // Deliberately not including a default selection.
                JOptionPane.showMessageDialog(null, "Option 4");
                break;
        }

    }

    public static void main(String[] args) // main program
    {
        new OnlineStore(); // Call out the program.
    }
}

Looping Menu

public class OnlineStore {

    // public static void main(String[] args) // main program

    public int display_menu() // Not the main program but the main menu.
    {
        String main_selection;

        main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
        return Integer.parseInt(main_selection);

    }

    public OnlineStore() // Switch-case program
    {
        boolean exit = false;
        do {
            switch (display_menu()) {
                case 1:
                    JOptionPane.showMessageDialog(null, "Option 1");
                    break;
                case 2:
                    JOptionPane.showMessageDialog(null, "Option 2");
                    break;
                case 3:
                    JOptionPane.showMessageDialog(null, "Option 3");
                    break;
                case 4:  // Deliberately not including a default selection.
                    JOptionPane.showMessageDialog(null, "Option 4");
                    break;
            }
        } while (!exit);
    }

    public static void main(String[] args) // main program
    {
        new OnlineStore(); // Call out the program.
    }
}

Upvotes: 1

Rafiq
Rafiq

Reputation: 750

Declare mSelect variable inside class not inside method.

public class OnlineStore {
int mSelect;
// public static void main(String[] args) // main program

public void display_menu() // Not the main program but the main menu.
{
    String main_selection;

    main_selection = JOptionPane.showInputDialog("Welcome!\n\n1. Add T-                Shirt Order\n2. Edit T-Shirt Order\n3. View Current Order\n4. Checkout\n\nPlease   enter your choice: ");
    mSelect = Integer.parseInt(main_selection);

}

public OnlineStore() // Switch-case program
{
    display_menu(); // Call display menu.
    switch (mSelect) {
        case 1:
            JOptionPane.showMessageDialog(null, "Option 1");
            break;
        case 2:
            JOptionPane.showMessageDialog(null, "Option 2");
            break;
        case 3:
            JOptionPane.showMessageDialog(null, "Option 3");
            break;
        case 4:  // Deliberately not including a default selection.
            JOptionPane.showMessageDialog(null, "Option 4");
            break;
    }

}

public static void main(String[] args) // main program
{
    new OnlineStore(); // Call out the program.
}}

Upvotes: 3

MrT
MrT

Reputation: 594

At the moment, without the Scanner, no integer is set to mselect - so your switch choose the case default if you would define one like:

switch(mSelect){
.
.
.
default: JOptionPane.showMessageDialog(null, "ERROR");
}

Another solution is that you set a default value to mSelect like

int mSelect = 1;

Upvotes: 0

Related Questions