Search code examples
javaswingswitch-statementjoptionpane

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.


Solution

  • 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.
        }
    }