user3235978
user3235978

Reputation: 13

Trying to create a menu and I get compiler error

So I am making a simple menu with Option dialog, but it just does not compile, not sure why.

This is the error:

Inventory.java:21: error: illegal start of expression
public static String promptInventory(String MName, String[] options)

Not sure what to do here. Also the way I have it set up, it should cycle back to the menu each time right? But I don't think it serves my purposes...

  import java.util.ArrayList;
  import javax.swing.JOptionPane;
  import javax.swing.JTextArea;
  import javax.swing.JScrollPane;

    class Inventory
    {
    public static void main(String arg[])
    {
        Database db = new Database();
        Database dpl = new Database();

    final String[] MENU_OPTIONS = {"exit", "Add product", "Sell product", "Delete  product", "Modify product",
                               "Display information"};
    final String MENU_NAME = "Inventory";


    String selection = promptInventory(MENU_NAME, MENU_OPTIONS);


    public static String promptInventory(String MName, String[] options)
    {
      int selection = JOptionPane.showOptionDialog(null, 
                                             "Enter your Transaction Type",
                                             MName,
                                             JOptionPane.DEFAULT_OPTION,
                                             JOptionPane.QUESTION_MESSAGE,
                                             null, options, options[0] );
      return (String)options[selection];
    }   


    //logic


      switch ( selection )
      {
         case  "exit"   :
                             break;

         case  "Add product"  : 
                             break;

         case  "Sell product" : 
                             break;

      }

      String selection = promptInventory(MENU_NAME, MENU_OPTIONS);   

      }

      }

Upvotes: 0

Views: 54

Answers (1)

Captain Giraffe
Captain Giraffe

Reputation: 14705

Use a tool that can format/indent the code for you. That makes these kind of errors obvious.

Your promptInventorymethod is now inside the main method and that is illegal.

Your class with methods should be indented like

class Inventory
{
    public void method(){

    } // end of method

    public void nextMethod(){
        // No methods in here. 
    }
}// end class

Upvotes: 1

Related Questions