tags:

views:

83

answers:

3

I'm using try on my code and it says illegal start of type. I'm using switch statements but default: continue; do not agree with each other I keep getting continue outside of loop. With the else statement it says illegal start type. So what can I do about try, continue, and the else statement.

   public class Menu {
private Inventory database;
private char menuItem;
private Scanner input;
private char mode;
int code;


public Menu(Inventory database)
{
    this.database = database;
    menuItem      = 'N';
    input = new Scanner(System.in);
}

public Menu(MyArrayList database)
{
    this.database = database;
    menuItem = 'A';
    input = new Scanner(System.in);

}

private void showMenu()
{
    if(code == 'A'){

    System.out.println();
    System.out.println("------------------");
    System.out.println("Display Movies : D");
     System.out.println("Add Movie      : A");
    System.out.println("Delete Movie   : X");
    System.out.println("Select Mode    : M");
    System.out.println("Exit           : E");
    System.out.println("------------------");
    System.out.println();
    System.out.print("Please make your selection: ");

}
else    
{
    System.out.println();
    System.out.println("------------------");
    System.out.println("Display Movies : D");
    System.out.println("Rent a Movie   : R");
    System.out.println("Reserve a Movie: S");
     System.out.println("Select Mode   : M");
    System.out.println("Exit           : E");
    System.out.println("------------------");
    System.out.println();
    System.out.print("Please make your selection: ");

}
}

private void rentMovie(int productID)
{
    int index = database.getIndex(productID);
    if( index == -1)
    {
        System.out.println("There is not such a code.");
    }
    else
    {
        if( database.getMovie(index).getIsRented())
        {
            System.out.println("You cannot rent " +  database.getMovie(index).getTitle() + ". It is already rented.");

        }
        else
        {
            database.getMovie(index).setIsRented(true);
            System.out.println("Please take your movie.");

        }
    }

}


private void reserveMovie(int productID)
{
    int index = database.getIndex(productID);
    if( index == -1)
    {
        System.out.println("There is not such a code.");
    }
    else
    {
        if( database.getMovie(index).getIsReserved() )
        {
            System.out.println("You cannot reserve " +  database.getMovie(index).getTitle() + ". It is already reserved.");
        }
        else
        {
            if( database.getMovie(index).getIsRented())
            {
                database.getMovie(index).setIsReserved(true);
                System.out.println( database.getMovie(index).getTitle() + " is reserved for you." );
            }
            else
            {
                System.out.println( database.getMovie(index).getTitle() + " is available. You can rent it if you like.");
            }
        }
    }

}

try{
    if(mode == 'A'){
        switch(menuItem){
            case 'N':
                break;
            case 'D':
                database.print();
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'A':
                String title;
                System.out.println("Enter movie title, then press enter");//movie title,
                title= input.nextLine();
                System.out.println("Enter movie code, then press enter");//enter movie code,then press enter
                code = Integer.parseInt(input.nextLine());
                addMovie(title,code);
                menuItem ='N';
                break;
            case 'X':
                System.out.println("");
                deleteMovie(code);
                menuItem ='N';
                break;
             case 'M':
                selectMode();
                menuItem = 'N';
             case 'E':
                System.out.print("Program terminated.");
                System.exit(0);
                break;
            default:
                continue;
        }
    }
}
   else
{
    public void run(){
           int code;
    while(true)
    {

        switch(menuItem)
        {
            case 'N':
                break;
            case 'D':
                database.print();
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'R':
                System.out.print("Please enter product code:");
                rentMovie( input.nextInt() );
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'S':
                System.out.print("Please enter product code:");
                reserveMovie( input.nextInt() );
                showMenu();
                menuItem = input.next().charAt(0);
                break;
            case 'E':
                System.out.print("Program terminated.");
                System.exit(0);
                break;
            default :
                showMenu();
                menuItem = input.next().charAt(0);
        }

    }
}

}

+1  A: 

You've got else following your try block, and that makes no sense. no, just bad indentation.

Where's your catch block, or finally block? It makes no sense to just have try. Also, you can't just declare a function in the middle of that else block.

Basically, I recommend you re-study the "Java Syntax" chapter of whatever guide you're using, because this code is just all kinds of wrong.

Pointy
I probably wrote the code wrong from my professor
Lola
Oh, well I don't have that. That's what I was thinking, "how is that there is try but no catch." I'm new at java so it gets confusing for me
Lola
Is a given assignment that our instructor gave us to add a switch statement
Lola
+1  A: 

A.. Few pointers.. First of all, your Try doesn't have a catch. Your case statements lack {} blocks. And you can't create a method within an else block.

Further to answer your Question: Illegal start of type means you haven't initiated the variable. For example "menuItem"

Edit: To further that, default should be break; not continue;

Edit2: And further your second switch contains a boolean as argument...

Meke
A: 

With your edit, now showing the issue. Your Try starts outside of a method body.

reserveMovie is closed just before try starts, and as such is not valid.

Meke
so I insert inside reserveMovie
Lola
You go back and study the code until you understand it. You Try shouldn't go anywhere as long as it doesn't have a catch
Meke