Search code examples
javacharswitch-statementjava.util.scannerdfa

Switch statement error representing DFA Java


I've written a Java program to represent a DFA using switch statement but it will not accept words it should do. I've tried adding a separate case to send the final state to to output 'word accepted' or 'word not accepted' but this didn't work. Example words accepted should be: google ggle xxgooooooglexeg

My code so far:

public static void main(String[] args) {

    System.out.println("Enter a word to run on the DFA:");
    Scanner scanner = new Scanner(System.in);
    String string = scanner.nextLine();
    int state = 1;
    for (char s : string.toCharArray()) {
        switch (state) {
            case (1): {
                if (s == 'e' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'g') {
                    state = 2;
                }
            }
            break;
            case (2): {
                if (s == 'e' || s == 'l' || s == 'x') {
                    state = 1; {
                } if (s == 'o') {
                    state = 2;
                } else if (s == 'g') {
                    state = 3;
                }  
                }
            }
            break;
            case (3): {
                if (s == 'e' || s == 'x') {
                    state = 1; {
                } if (s == 'g' || s == 'o') {
                    state = 2;
                } else if (s == 'l') {
                    state = 4;
                }
            }
            break; }
            case (4): {
                if (s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'e') {
                    state = 5;
            }
            break; }
            case (5): {
                if (s == 'e' || s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 5;
                } else {
                    state = 5;
                }
            break; }

        }
    }

    if (state == 5) {
        System.out.println("Word accepted");
    } else {
        System.out.println("Word not accepted");
        scanner.close();
    }

}

P.S: I know if else statements are slow but for a small program like this it seems to be quick enough.


Solution

  • You have a lot of issues in your code. Try indenting your code properly and see where you are opening and closing the brackets.

    For example:

    • Your break is outside of the case block in case 1 and 2.
    • You have an empty block in case 2 and 3 after this statement state = 1; {}
    • In case 2 and 3, your if-else if is enclosed in the 1st if statement. I am assuming this is wrong, given your input and expected output.

    Here is the formatted code which works:

    for (char s : string.toCharArray()) {
            switch (state) {
            case (1): {
                if (s == 'e' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'g') {
                    state = 2;
                }
                break;
            }
    
            case (2): {
                if (s == 'e' || s == 'l' || s == 'x') {
                    state = 1;
                } else if (s == 'o') {
                    state = 2;
                } else if (s == 'g') {
                    state = 3;
                }
    
                break;
            }
    
            case (3): {
                if (s == 'e' || s == 'x') {
                    state = 1;
                } else if (s == 'g' || s == 'o') {
                    state = 2;
                } else if (s == 'l') {
                    state = 4;
                }
    
                break;
            }
            case (4): {
                if (s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 1;
                } else if (s == 'e') {
                    state = 5;
                }
                break;
            }
            case (5): {
                if (s == 'e' || s == 'g' || s == 'l' || s == 'o' || s == 'x') {
                    state = 5;
                } else {
                    state = 5;
                }
                break;
            }
    
            }
        }