Javatpoint Logo
Javatpoint Logo

Orphaned Case Java

In the previous section, we have discussed the switch statement in detail. In this section, we are going to discuss the rarest error i.e. orphaned case error in Java.

Orphaned Case Error

In Java, it is the rarest error that does not occur usually. The error occurs while dealing with the switch statement.

Reasons to Raise Orphaned Case Error

1. The keyword switch may be capitalizing. For example:

2. The cases may be out of the switch statement. For example, in the following code snippet case 2 is out of the switch statement.

3. If switch statement unexpectedly terminated.

4. If we use comparison expression in the case statement.

How to fix an orphaned case error?

  • The keyword switch should be in small letters.
  • Ensure that all the case statements are inside the switch statement.
  • The switch statement should not be terminated by a semicolon (;).
  • Case statement should have number (1, 2, 3, …), character ('a', 'A', …), string ("Sunday", "act2102").
  • Case statement should not have a conditional expression like x>1.
  • Beside this, we can also use the four wrapper classes (Byte, Short, Integer, and Long) in switch and case statements. For example, consider the following code snippet.

Example of Orphaned Case Error

OrphanedCaseError.java

When we compile the above Java program, it gives an orphaned case error.

Orphaned Case Java

In order to overcome the above error, write the switch statement in small letters. After making the changes, compile and run the above code. It gives the following output:

You are eligible for vote.

Let's see another Java program that raises orphaned case errors.

OrphanedCaseErrorExample.java

When we compile the above program, it gives an orphaned case error because the cases having uppercase letters are outside the switch statement.

Orphaned Case Java

We can resolve the above error by wrapping the case statements inside the switch statement.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA