摩尔斯电码英文:ArrayIndexOutOfBoundsException

时间:2015-10-04 18:13:15

标签: java indexoutofboundsexception

我正在尝试用Java创建一个将莫尔斯代码转换为英语的程序。在这种情况下,字母用一个空格分隔,单词用三个空格分隔:

import java.util.Scanner;

public class ReverseMorseCodeProgram {
  public static void main(String[] args) { //Converts Morse Code into English

  System.out.println("Enter the Morse Code to be converted to English (letters and spaces only, no numbers or punctuation):");
  String sentence = new Scanner(System.in).nextLine(); //Input is converted into String
  char[] dotdash = sentence.toCharArray(); //String is converted into char[]
  String[] words = new String[30]; //String array where char[] is converted into morse code letters
  int y = 0;
  int x = 0;

  while (dotdash[x] < dotdash.length) { //Converts char[] into String[]

    while (dotdash[x] != ' ') { //loops until a space is encountered
      words[y] = words[y] + dotdash[x]; //adds chars to String in array
      x = x + 1; //goes to next char in array
    }

    if ((dotdash[x+1] == ' ') && (dotdash[x+2] == ' ')) { //determines whether there are three spaces in a row
      words[y+1] = "   "; //adds "   " as next string in array
      y = y + 2; //moves to string after "   "
      x = x + 3; //moves to char after the three spaces
    }
    else { //if there's only one space
      y = y + 1; //moves to next string in String[]
      x = x + 1; //moves to next char in char[]
    }
  }

  char[] alphabet = {'a', 'b', 'c', 'd', //English alphabet array
    'e', 'f', 'g', 'h',
    'i', 'j', 'k', 'l',
    'm', 'n', 'o', 'p',
    'q', 'r', 's', 't',
    'u', 'v', 'w', 'x',
    'y', 'z', ' '};
  String[] morse = {".-", "-...", "-.-.", "-..", //morse code alphabet array
    ".", "..-.", "--.", "....",
    "..", ".---", "-.-", ".-..",
    "--", "-.", "---", ".--.",
    "--.-", ".-.", "...", "-",
    "..-", "...-", ".--", "-..-",
    "-.--", "--..", "   "};


  for (int i = 0; i < words.length; i++) { //repeats until end of String array
    for (int t = 0; t < 27; t++) { //goes through morse code array
      if (morse[t] == words[i]) { //compares morse code to each word in String array
        System.out.print(alphabet[t]); //prints equivalent english letter when match is found
      }
    }
    }
  }
}

然而,当我输入短语“ - -.-- ..- ..- ...- --- ..- .- .. ... - .- .-。--- .-。 .-- .- .. .. ... .- .. - ..“,我收到以下错误:

java.lang.ArrayIndexOutOfBoundsException: 78
    at ReverseMorseCodeProgram.main(ReverseMorseCodeProgram.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

输入较短的字符串,例如“ - ”会导致根本没有输出。

我对编程很新,这让我很难过。

编辑:我已经改变了违规部分:

  while (dotdash[x] < dotdash.length) { //Converts char[] into String[]

    while (dotdash[x] != ' ') { //loops until a space is encountered
      words[y] = words[y] + dotdash[x]; //adds chars to String in array
      x = x + 1; //goes to next char in array
    }

    if ((dotdash[x+1] == ' ') && (dotdash[x+2] == ' ')) { //determines whether there are three spaces in a row
      words[y+1] = "   "; //adds "   " as next string in array
      y = y + 2; //moves to string after "   "
      x = x + 3; //moves to char after the three spaces
    }
    else { //if there's only one space
      y = y + 1; //moves to next string in String[]
      x = x + 1; //moves to next char in char[]
    }
  }

以下内容:

  while (x < dotdash.length) { //Converts char[] into String[]

    while (dotdash[x] != ' ') { //loops until a space is encountered
      words[y] = words[y] + dotdash[x]; //adds chars to String in array
      x = x + 1; //goes to next char in array
    }

    if (((x+2) < dotdash.length) && ((y+1) < words.length)) { //ensures that dotdash[x+2] and (y+1) doesn't exceed their respective boundaries

      if ((dotdash[x+1] == ' ') && (dotdash[x+2] == ' ')) { //determines whether there are three spaces in a row
        words[y+1] = "   "; //adds "   " as next string in array
        y = y + 2; //moves to next string after "   "
        x = x + 3; //moves to next char after the three spaces
      }
      else { //if there's only one space
        y = y + 1; //moves to next string in String[]
        x = x + 1; //moves to next char in char[]
      }

    }

  }

但是,ArrayIndexOutOfBoundsException错误仍然存​​在。我似乎无法发现我可以超越阵列界限的其他地方。

2 个答案:

答案 0 :(得分:0)

在第15行,将while(dotdash [x]&lt; dotdash.length)更改为while(x&lt; dotdash.length)。 dotdash [x]是带有索引x的dotdash的元素。您不希望将其与数组的长度进行比较,您希望将索引(x)与数组的长度进行比较。

可能还有其他问题,我没有超越那个问题,因为这是引起异常的原因。

在编辑时:再看一下,你有很多地方可以发生ArrayIndexOutOfBoundsException。你获取数组元素的任何地方,所以dotdash [x + 1]等等,如果你想要得到的元素超出了数组的末尾,你将得到一个ArrayIndexOutOfBoundsException,所以你需要制作确定x + 1&lt;在尝试获取dotdash [x + 1]等之前的dotdash.length

答案 1 :(得分:0)

while (dotdash[x] < dotdash.length)

这对我来说没有任何意义(乍一看)。数组中的值不是数字。

我认为你的意思是:

while (x < dotdash.length)

因此,您可以索引数组中的所有条目。

当然,一旦你这样做,你现在会遇到以下问题:

if ((dotdash[x+1] == ' ') && (dotdash[x+2] == ' '))

因为“x + 1”和“x + 2”可以大于数组的长度。

因此if语句只能在x&lt; dotdash.length - 2.

所以你需要重新编码一下代码。