2015-10-04 141 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

所以,你需要重組你的代碼位。