2015-10-04 147 views
1

我正在尝试创建一个Java程序,将莫尔斯码转换为英文。在这种情况下,字母用一个空格分开,以字由三个空格隔开。英文摩尔斯码:ArrayIndexOutOfBoundsException

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异常错误仍然存​​在。我似乎无法找到我能超越阵列边界的地方。

回答

0

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

可能还有其他的问题,我没有超越那一个,因为这是什么导致了异常。

关于编辑:进一步观察后,您可能会发生一些ArrayIndexOutOfBoundsException异常。如果你想要获取的元素超出数组的末尾,那么你可以使用数组的元素,比如dotdash [x + 1]等等,你会得到一个ArrayIndexOutOfBoundsException,所以你需要试图让dotdash [X + 1]之前,请确保X + 1个< dotdash.length等

+0

我已经做了一些改变(添加到我的文章的编辑),但错误似乎是持续存在... –

+0

ArrayIndexOutOfBoundsException会告诉您发生异常的线路,因此请检查该线路上的代码,以查看超出数组末尾的引用方式。我猜它会在第一个内部while循环中的某处,在那里你不检查任何长度。 – blm

0
while (dotdash[x] < dotdash.length) 

这不会让(乍一看我)任何意义。数组中的值不是数字。

我想你的意思是:

while (x < dotdash.length) 

所以,你可以索引所有的数组中的条目。

当然,一旦你做,你现在有问题:

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

,因为“X + 1”和“x + 2”可以比数组的长度。

所以,如果当x < dotdash.length只能执行的语句 - 2

所以,你需要重组你的代码位。