AMR
AMR

Reputation: 21

Assign value to variable in Java with Switch Stament

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

Upvotes: -1

Views: 1912

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 339855

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.


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

Upvotes: 4

pebble unit
pebble unit

Reputation: 1224

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)

Upvotes: 1

Related Questions