0

Im a beginner in the coding world. I have been learning Java recently when i came across a speedbump.

First of all heres the code :

import  java.util.*;
public class Stuff {

    public static void main(String []args); {


        Scanner identity = new Scanner(System.in);

        String id;

        System.out.println("Please Enter Your Name :");
        id = identity.next();

        Switch (id); {

            case "name1":
            //some code here....
            break; 

            case "name2":
            //some code here....
            break;

            case "name3":
             //some code here....
            break;

            case "name4":
            //some code here....
            break;

            default :
            //some code here....
            break;
        }


    }

}

The error

 Error: Orphaned case
         case: "name1";

I cant seem to find why this is happening and have googles to no avail.

Edit : Some people have said that I am ending Switch early with the semi colon. But when i add it, i get a new error along with the previous one:

Error: ';' expected
        Switch (id) {
              ^ 
5
  • 1
    You have a semicolon after Switch (id). Take it out. Also, switch needs to be lower case. Commented Sep 15, 2015 at 13:39
  • 1
    You've put a semi-colon behind switch(id). This ends the switch statement which you did not intend. Commented Sep 15, 2015 at 13:40
  • have googles to no avail. really? the first 5 hits describe exactly your problem :D Commented Sep 15, 2015 at 13:41
  • Can you see the difference between case: "name1"; and case "name1":? Commented Sep 15, 2015 at 13:41
  • also public static void main(String []args); { remove this semicolon too Commented Sep 15, 2015 at 13:43

5 Answers 5

4

You ran into multiple problems here.

Problem 1 :

 Switch (id); {

 ----------^

Look carefully your ; ends your switch there right away.

Apparently all your case statements became orphans :)

Problem 2 :

Your Switch should be switch(lower case s)

Problem 3 :

One more ; cause you compile time error at the line

public static void main(String []args); {


                                 -----^

Note: I strongly suggest you to use an IDE, to save lot of time here. It tells you the compiler errors on the fly.

Sign up to request clarification or add additional context in comments.

4 Comments

Also, switch needs to begin with a lowercase s
@BackSlash Mentioned it. :)
Thanks! Haha, im pretty embarassed that i made such dumb mistakes.
@AryanSrivastava Don't worry, we too came from that level ;)
2

Your syntax for the switch statement is wrong.

switch (id) {
    case "name1":
        //some code here....
        break; 

    case "name2":
        //some code here....
        break;

    case "name3":
        //some code here....
        break;

    case "name4":
        //some code here....
        break;

    default :
        //some code here....
        break;
}

Comments

2

What you try will need a quite current version of Java because you use Strings with switch.

And you have to

switch (id)

So please remove the ;

Comments

1

The semicolon after the Switch (id);statement effectively terminates the switch case and the cases you define afterwards are orphan(i.e. without any switch case)

Comments

1

You are finishing the switch statement early:

Switch (id); {

The real syntax is:

switch (id) {
  // your cases
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.