0

I am working on the game where the columns of the board are represented by the characters but i would like to assign them an index. I have decided to use the switch statement in that case, however it does produce the wrong result. With the current code I attach, it gives me 14 as an index, however since the string is 7h, and it takes h as a char, it should give an index of 7. What Could be an issue? Thanks in advance!

public class Check {
    
    public int columnToInt(char c) {
        int index=0;
        switch(c) {
        case 'a':
            index=0;
        case 'b':
            index=1;
        case 'c':
            index=2;
        case 'd':
            index=3;
        case 'e':
            index=4;
        case 'f':
            index=5;
        case 'g':
            index=6;
        case 'h':
            index=7;
        case 'i':
            index=8;
        case 'j':
            index=9;
        case 'k':
            index=10;
        case 'l':
            index=11;
        case 'm':
            index=12;
        case 'n':
            index=13;
        case 'o':
            index=14;
        }
        return index;
        }


          public static void main(String[] args) {
            String myStr = "7h";
            char c =myStr.charAt(1);
            System.out.println("the char at position 1 is "+c);
            Check check = new Check();
            int result = check.columnToInt(c);
            System.out.println(result);
          }
        }

Edvin
  • 11
  • 3
  • 3
    you forgot to use `break` in each case – sittsering Jan 07 '22 at 17:56
  • Do not use switch statements, do what @ScottHunter sent you. Also your code is wrong because you don't use breaks after each case. – ErayZaxy Jan 07 '22 at 17:57
  • @ScottHunter the OP is not converting a `char` into its ASCII value. The OP is looking kind of simulating locating the given `char` in a `char[]`. – hfontanez Jan 07 '22 at 18:02
  • I have a better solution, but the question is closed. So, here is my recommendation to you. Create a list of characters like this as a global field of the `Check` class: `private List charList = List.of('a','b','c','d','e','f','g','h','i','l','m','m','o','p','q','r','s','t','v','x');`. Then, inside your `columnToInt(char)` method include JUST the following line: `return charList.indexOf(c);` The character array will eliminate the need for the `switch` and the method will make use of the `indexOf()` method to return the location of the character in the list. – hfontanez Jan 07 '22 at 18:56
  • @hfontanez: `(int)c - (int)'a'` – Scott Hunter Jan 07 '22 at 19:12
  • @ScottHunter that only works by coincidence. If the characters are not in sequential order in the ASCII table that doesn't work. I understood what you meant, but I still believe that's not what the OP asked for. I believe my reply to you was because you suggested that the OP was a duplicate of another post looking to convert `char` to `int` based on ASCII values. This is based on an index. Two different problems. – hfontanez Jan 07 '22 at 20:10
  • @hfontanez: But in *this* question (which is all I am addressing), they *are* in sequential order, and as such, conversion to ASCII (with an offset) solves the *problem as stated*. (And "the ASCII table" by definition is in sequential order (part of what makes it ASCII.) – Scott Hunter Jan 07 '22 at 20:14
  • @ScottHunter Agreed. It solves _this_ (specific) problem because the solution is not abstracted. My solution is abstracted and works with _this_ problem **and** with other similar problems. But, back to my point, it wasn't a duplicate of the question you proposed when you suggested it was a duplicate. It wasn't. It was _similar_, but not a duplicate. That was my point. – hfontanez Jan 07 '22 at 20:59
  • Much easier to use a [switch expression](https://openjdk.java.net/jeps/361). – Basil Bourque Jan 08 '22 at 06:22

2 Answers2

2

Java switch statements can be a bit annoying to use. You need to use break or all the cases after the expected one will be executed as well.

switch(c) {
    case 'a':
        index=0;
        break;

Alternatively you can use a return.

switch(c) {
    case 'a':
        return 0;
magicmn
  • 1,787
  • 7
  • 15
0

You must add the break keyword for each case.

For example:

case 'a':
   index=0;
   break;

otherwise next assignments are applied.