Search code examples
javaexceptionmorse-code

Morse Code to English Translator gives ArrayIndexoutofBounds


I have to create morse to English and vice versa translator. The English to morse part works but whenever I try to enter something in morse it gives me an ArrayIndexOutofBounds exception and I am stuck with how to fix it. I have put in a split function, but I am just unsure why I get the exception.

public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char[] english = { 'a', 'b', 'c', 'd', 'e', 'f',
                            'g', 'h', 'i', 'j', 'k', 'l',
                            'm', 'n', 'o', 'p', 'q', 'r',
                            's', 't', 'u', 'v', 'w', 'x',
                            'y', 'z', '1', '2', '3', '4',
                            '5', '6', '7', '8', '9', '0'};
        String[] morse = { ".-",   "-...", "-.-.", "-..",  ".",
                            "..-.", "--.",  "....", "..",   ".---",
                            "-.-",  ".-..", "--",   "-.",   "---",
                            ".--.", "--.-", ".-.",  "...",  "-",
                            "..-",  "...-", ".--",  "-..-", "-.--",
                            "--..", "|" }
        String userInput;
        int translatorChoice;
        String result; 
        System.out.println("Enter 1 for English to Morse code. Enter 2 for Morse to English:");
        translatorChoice = input.nextInt();
        if (translatorChoice != 1 && translatorChoice !=2 ){
            throw new ArithmeticException("Please enter a valid number");
        }
        System.out.println();
        System.out.println("Please input sentence into the translator:");
        userInput = br.readLine();

        if (translatorChoice == 1){
            userInput = userInput.toLowerCase();
            englishtoMorse(morse,userInput,english);}
        else if(translatorChoice == 2) {
            morsetoEnglish(english, userInput, morse);}
public static void morsetoEnglish (char[] english, String userInput, String[] morse){
        String[] input = userInput.split("|");
        for (int i = 0; i < input.length; i++){
            for (int j = 0; j < morse.length; i++){
                if (morse[j].equals(input[i])) {    
                    System.out.print(english[j]);
                }}}}

Solution

  • Here's working code. 3 changes were to be made, first of all, split over "|" had to be changed to correct regex "\\|" to literally split on '|'. split() takes regular expression as an argument and '|' is a special character in regex, so you need to escape it with regexe's \, but then, when translating regex to java string you escape it again. The result is \\|
    Second, inner for loop can stop when strings matched, thus break added.
    Third, variable i changed to j in inner for loop incrementation.

        public static void morsetoEnglish (char[] english, String userInput, String[] morse){
            String[] input = userInput.split("\\|");
            for (int i = 0; i < input.length; i++){
                for (int j = 0; j < morse.length; j++){
                    if (morse[j].equals(input[i])) {    
                        System.out.print(english[j]);
                        break;
                    }
                }
                
            }
        }
    

    Here's some test input/output:

    Enter 1 for English to Morse code. Enter 2 for Morse to English:
    2
    Please input sentence into the translator:
    .--.|.|-|
    pet