user2860559
user2860559

Reputation: 1

Can I instantiate a switch? Or what is wrong with this?

So, so far everything is going swell, because I never worked on a personal project with multiple classes before, and I'be been able to fix everything myself when it's given me errors so far. I'm not sure wiether or or not I can instantiate a switch statement, or if I'm actually using the right vocabulary for what I'm trying to do. And if I can't can I get some help on what I can change to make this work and/or help me in the future? Here is my code:

import java.io.*;
import java.util.*;

public class SetUp_Menu    {
    public switch setup_menu;

    public static void main(String args[]){
        System.out.println("Welcome to setup. ");
        System.out.println("1. First setup");
        System.out.println("2. I didn't mean to setup, go back");

        switch(setup_menu){
            case 1:
                SetUp first = new SetUp();
            }
        }
    }   
}

And here is my error:

File: C:\Users\NoNeedForThisToBeHere\Ruby\SetUp_Menu.java [line: 6]
Error: illegal start of type
File: C:\Users\NoNeedForThisToBeHere\Ruby\SetUp_Menu.java [line: 6]
Error: ';' expected

Any help would be much appreciated thanks!

Upvotes: 0

Views: 130

Answers (4)

Nick Louloudakis
Nick Louloudakis

Reputation: 6015

In Java, you can use an int or a String (from Java 7) as a switch parameter for switch statements, and not "switch" objects.

For example, you can do to get a number from standar input and to use switch with it:

import java.io.*;
import java.util.*;

public class SetUp_Menu
{

     public static void main(String args[])
     {
       System.out.println("Welcome to setup. ");
       Scanner in = new Scanner(System.in);
       int menu_choice = in.nextInt();
       switch(menu_choice)
       {
         case 1:
           System.out.println("1. First setup");
           break;
         case 2:
           System.out.println("2. Second setup");
           break;
         default:
           System.out.println("This setup case will be executed " + 
                              "if not any else case is met (optional).");
           break;

       }
    }
}

Please notice the break; line inside each case. This is because if not exited from the switch statement, the cases below the case chosen will be executed (the control flow will continue) till a break or return command is found or all cases are executed.

See here for more information: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Upvotes: 0

sunysen
sunysen

Reputation: 2351

try

  public class SetUp_Menu {
    // modify this line public switch setup_menu;
    public static int setup_menu = 0;

    public static void main(String args[]) {
        System.out.println("Welcome to setup. ");
        System.out.println("1. First setup");
        System.out.println("2. I didn't mean to setup, go back");

        switch (setup_menu) {
        case 1:
            SetUp first = new SetUp();
            break;
        default:
            break;
        }
    }
}

You don't instantiate a switch object. You can declare an int, and enum,and byte,and char,and short, or in Java 7+, a String

Upvotes: 0

Omar Mainegra
Omar Mainegra

Reputation: 4194

switch is a reserved word of Java (and many other) language for Control Flow ( http://en.wikipedia.org/wiki/Control_flow#Case_and_switch_statements), not a type that you can instantiate, nor a valid identifier you can use as a variable name.

Upvotes: 2

rgettman
rgettman

Reputation: 178303

You don't instantiate a switch object. You can declare an int, and enum, or in Java 7+, a String and you can switch on that. Based on your menu, an int is a good choice.

public int setup_menu;

You will need to populate setup_menu with the user's choice.

Upvotes: 2

Related Questions