-1

I am trying to assign value to variable based on case with old Switch case it is not allowing is there any way or should i need to write initialize variable in each single case?

public static void printNumberInWord(int number){
        String inWord = switch(number){
            case 0 -> "ZERO";

            case 1: "ONE";
            break;
            case 2: "TWO";
            break;
            case 3: "THREE";
            case 4: "FOUR";
            case 5: "FIVE";
            case 6: "SIX";
            case 7: "SEVEN";
            case 8: "EIGHT";
            case 9: "NINE";
            default : "no Value"

        };

enter image description here

Enchanced Switch is working though as shown in Case0

4
  • 1
    Are you asking how to return a value from a switch statement? Your Question is not clear. Edit for clarity. Commented Jan 6, 2024 at 8:21
  • 1
    Of course, if you're really doing that, it would be a lot better to do: String[] values = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" }; and address only that range Commented Jan 6, 2024 at 10:31
  • by the way, that is not an enhanced switch according the Java Language Specification: "An enhanced switch statement is one where either (i) the type of the selector expression is not char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type, or (ii) there is a case pattern or null literal associated with the switch block." Commented Oct 26, 2024 at 18:49
  • 1
    Voting to re-open. This Question is not caused by a mere typographical mistake. This Question is about misunderstanding the various kinds of switch statements and switch expressions. These kinds of switch in Java have evolved dramatically. The topic is understandably confusing. Commented 4 hours ago

2 Answers 2

7

tl;dr

You incorrectly mixed use of COLON character and the arrow (HYPHEN-MINUS & GREATER THAN).

In a switch expression, use only the arrow, ->.

Switch Expressions

In Java 14+, use switch expressions to return a value. See JEP 361: Switch Expressions.

int number = 7;
String word =
        switch ( number )
        {
            case 0 -> "ZERO";
            case 1 -> "ONE";
            case 2 -> "TWO";
            case 3 -> "THREE";
            case 4 -> "FOUR";
            case 5 -> "FIVE";
            case 6 -> "SIX";
            case 7 -> "SEVEN";
            case 8 -> "EIGHT";
            case 9 -> "NINE";
            default -> "N/A";
        };

word = SEVEN

Notice: no COLON :, no break.

Separate method

As another example, move that switch into a method.

private static String digitToWord ( final int x )
{
    return
            switch ( x )
            {
                case 0 -> "ZERO";
                case 1 -> "ONE";
                case 2 -> "TWO";
                case 3 -> "THREE";
                case 4 -> "FOUR";
                case 5 -> "FIVE";
                case 6 -> "SIX";
                case 7 -> "SEVEN";
                case 8 -> "EIGHT";
                case 9 -> "NINE";
                default -> "N/A";
            };
}

Exercise that method:

IntStream
.rangeClosed ( 0 , 10 )
.forEach ( 
    ( int i ) -> System.out.println ( i + " = " + digitToWord ( i ) ) 
);

Output:

0 = ZERO
1 = ONE
2 = TWO
3 = THREE
4 = FOUR
5 = FIVE
6 = SIX
7 = SEVEN
8 = EIGHT
9 = NINE
10 = N/A
Sign up to request clarification or add additional context in comments.

Comments

2

yield

If you insist on using the older switch statements (not switch expressions). You would use the yield keyword to return the result as the value of the switch statement.

int number = 1;
        String inWord = switch(number){
            case 1: yield "ONE";
            case 2: yield "TWO";
            case 3: yield "THREE";
            case 4: yield "FOUR";
            case 5: yield "FIVE";
            case 6: yield "SIX";
            case 7: yield "SEVEN";
            case 8: yield "EIGHT";
            case 9: yield "NINE";
            default : yield "no Value";

        };

        System.out.println(inWord);

Note that it does not use break; and that you cannot mix the two different types together (switch statements and switch expressions)

1 Comment

that is still a switch expression - (switch) statements do not have any result and can not be used in an assignment. You meant older switch labeled statement group; versus switch rule (JLS 15.28. switch Expressions)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.