-3

I have a case statement here that I use:

  String color = this.getPlayer().getColor();

    System.out.print("\nCOLOR IS: " + color + "\n");

    switch(color) {
        case "Black":
            /*
            we need the front two diag pieces. Are they opposition color?

             */

            Piece p = board[fromX - 1][fromY - 1].getPiece();
            if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
                if (p instanceof Pawn) {
                    return true;
                }
            }

            p = board[fromX + 1][fromY - 1].getPiece();
            if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
                if (p instanceof Pawn) {
                    return true;
                }}

        case "White":
            System.out.print("\n\nYOU ARE IN WHITE CASE STATEMENT");



            p = board[fromX - 1][fromY + 1].getPiece();
            if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
                if (p instanceof Pawn) {
                    return true;
                }
            }

            p = board[fromX + 1][fromY + 1].getPiece();
            if ((p != null) & (p.getPlayer().getColor() != this.getPlayer().getColor())) {
                if (p instanceof Pawn) {
                    return true;
                }
            }}

and running this code will land me in the white case even though the String color variable is definitely Black. here is proof of this fact

    COLOR IS: Black


YOU ARE IN WHITE CASE STATEMENTException in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
    at King.underThreat(King.java:233)
    at Game.move(Game.java:118)
    at main.main(main.java:19)

Process finished with exit code 1

This means that this.getPlayer().getColor() is definitely "black" and that it is going into the opposite case in the switch statement that I have set up. Can anyone please explain the situations in which this would occur? it seems very illogical to me.

entercaspa
  • 674
  • 2
  • 7
  • 19
  • add a `break;` after you're done handling black. – Matthew Nov 05 '18 at 20:04
  • You're missing `break;` at the end of each of your `case:` – Cory Fail Nov 05 '18 at 20:04
  • understood, will do that. But it still does not make logical sense that this occurs no? – entercaspa Nov 05 '18 at 20:05
  • 1
    Or in other words, it's executing both cases (not just the white) because the black case is falling through due to the lack of `break`. Put a print in the black case as well to see. – Carcigenicate Nov 05 '18 at 20:05
  • `As break statement is optional. If we omit the break, execution will continue on into the next case. It is sometimes desirable to have multiple cases without break statements between them. For example, consider the updates version of above program, it also display whether a day is a weekday or a weekend day.` From: https://www.geeksforgeeks.org/switch-statement-in-java/ – Cory Fail Nov 05 '18 at 20:06
  • 1
    Possible duplicate of [In a switch statement, why are all the cases being executed?](https://stackoverflow.com/questions/8058995/in-a-switch-statement-why-are-all-the-cases-being-executed) – GBlodgett Nov 05 '18 at 20:07
  • Understood guys thank you! – entercaspa Nov 05 '18 at 20:07

2 Answers2

2

You never have a break in your code:

switch (color) {
case "Black":
    // Code
    break;

case "White":
    // Code
    break;
}
Mark
  • 5,089
  • 2
  • 20
  • 31
  • does this mean that without the break the JVM finds a case and will execute from that point on, thus always visiting the proceeding case statements if there is no break? – entercaspa Nov 05 '18 at 20:06
  • @entercaspa Without breaks, it will execute every case under the case that it matched against. – Carcigenicate Nov 05 '18 at 20:08
0

You have this issue because you did not use break, without it, it will goes down to all the cases. The correct syntax is:

switch(color) {
    case "Black":
    // your code
       break;
    case "White":
    // your code
       break;
}
Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96