Home > Software design >  problem with "String line = switch (a) in java loop. Cant implement cases
problem with "String line = switch (a) in java loop. Cant implement cases

Time:12-16

I made a program of tic-tac-toe. There is a problem on the line String line = switch(a) ("illegal start of expression").

It is a window application of this game.

I have no idea how to fix this.

public void checkIfGameIsOver(){
    for (int a = 0; a < 8; a  ) {
        String line = switch (a) {
            case 0 -> button1.getText()   button2.getText()   button3.getText();
            case 1 -> button4.getText()   button5.getText()   button6.getText();
            case 2 -> button7.getText()   button8.getText()   button9.getText();
            case 3 -> button1.getText()   button5.getText()   button9.getText();
            case 4 -> button3.getText()   button5.getText()   button7.getText();
            case 5 -> button1.getText()   button4.getText()   button7.getText();
            case 6 -> button2.getText()   button5.getText()   button8.getText();
            case 7 -> button3.getText()   button6.getText()   button9.getText();
            default -> null;
        };

I'm using Java version 1.8.0_231.

CodePudding user response:

Switch expressions were introduced in Java 12 as a preview feature and formally made available in Java 13 (JEP 354). Given you're using Java 8, you cannot use switch expressions.

You either need to upgrade to a more recent Java version (e.g. Java 17), or use a switch statement.

That is:

public void checkIfGameIsOver(){
    for (int a = 0; a < 8; a  ) {
        String line;
        switch (a) {
        case 0:
            line = button1.getText()   button2.getText()   button3.getText();
        case 1:
            line = button4.getText()   button5.getText()   button6.getText();
        case 2:
            line = button7.getText()   button8.getText()   button9.getText();
        case 3:
            line = button1.getText()   button5.getText()   button9.getText();
        case 4:
            line = button3.getText()   button5.getText()   button7.getText();
        case 5:
            line = button1.getText()   button4.getText()   button7.getText();
        case 6:
            line = button2.getText()   button5.getText()   button8.getText();
        case 7:
            line = button3.getText()   button6.getText()   button9.getText();
        default:
            line = null;
        };

CodePudding user response:

It is not supported in Java 8. However, you can replace that with conditional expressions that are supported in Java8 which will look very similar.

public void checkIfGameIsOver(){
    for (int a = 0; a < 8; a  ) {
        String line = a==0 ? button1.getText()   button2.getText()   button3.getText() :
            a==1 ? button4.getText()   button5.getText()   button6.getText():
            a==2 ? button7.getText()   button8.getText()   button9.getText():
            a==3 ? button1.getText()   button5.getText()   button9.getText():
            a==4 ? button3.getText()   button5.getText()   button7.getText():
            a==5 ? button1.getText()   button4.getText()   button7.getText():
            a==6 ? button2.getText()   button5.getText()   button8.getText():
            a==7 ? button3.getText()   button6.getText()   button9.getText():
            null;
        };

With the disclaimer that a==7 is optional as it is always true on last iteration

so it could be

public void checkIfGameIsOver(){
    for (int a = 0; a < 8; a  ) {
        String line = a==0 ? button1.getText()   button2.getText()   button3.getText() :
            a==1 ? button4.getText()   button5.getText()   button6.getText():
            a==2 ? button7.getText()   button8.getText()   button9.getText():
            a==3 ? button1.getText()   button5.getText()   button9.getText():
            a==4 ? button3.getText()   button5.getText()   button7.getText():
            a==5 ? button1.getText()   button4.getText()   button7.getText():
            a==6 ? button2.getText()   button5.getText()   button8.getText():
            button3.getText()   button6.getText()   button9.getText();
        };

CodePudding user response:

First solution:

Declare String line first and then just set the value with the switch expression.

case 0 -> line = /*getText*/

Second solution:

Are you using any jdk with a smaller version than 13? Then that is the issue, due to switch expressions only being added in Java 13 and above

Side note

If you want to use String line outside of your for loop, you'd have to declare the variable outside of the for loop

  • Related