2011-04-03 38 views
1

莫爾斯電碼翻譯的價值不斷返回null我已經盡我所能去解決它。我們被限制在這個任務上使用數組。我能做些什麼來糾正它?幫助摩斯碼Java分配!

import javax.swing.JOptionPane; 
import java.util.*; 
import java.io.*; 

public class morseCodeTest 
{ 
    public static void main(String[] args)throws IOException 
    { 
     String userInput; 
     final String SENTINEL = "0";//for exiting program when entered 

     //object creation 
     Translate text = new Translate(); 

     //getting user input to be translated 
     do 
     { 
      userInput = JOptionPane.showInputDialog("Please enter what you wish to translte to Morse code (no punctuation)."); 
      String compare = userInput.toUpperCase(); 
      String[] codedText = new String[compare.length()]; 

      codedText = text.translateHere(compare); 
      text.toString(userInput, codedText); 
     }while(!userInput.equals(SENTINEL)); 
    }//end main 
}//end class 

class Translate 
{ 
    public Translate() 
    { 
    }//end default constructor 

    public String[] translateHere(String s)throws IOException 
    { 
     String compare = s, codedLine = ""; //userInput toUpperCase 
     int length = compare.length(); //length of userInput 
     String line, file = "Morse.txt";// variable holding file name and variable for each letter/number 
     char code; 

     //Constants 
     final int MAX = 36; 

     //Arrays 
     char[] morseLetter = new char[MAX]; 
     String[] morseCode = new String[MAX]; 
     String[] newMessage = new String[length]; 

     //putting user input in a character array; 
     char[] userLetters = compare.toCharArray(); 

     //object creation 
     File openFile = new File(file); 
     Scanner inFile = new Scanner(openFile); 

     //for loop that will read data from the Morse.txt file 
     for(int i = 0; i < MAX; i++) 
     { 
      while(inFile.hasNext()) 
      { 
       line = inFile.next(); 
       code = (char)line.charAt(0); 
       morseLetter[i] = code; 
       morseCode[i] = inFile.next(); 
      }//end nested while loop 
     }//end for loop 

     for(int j = 0; j < length; j++) 
     { 
      for(int k = 0; k < MAX; k++) 
      { 
       if(userLetters[j] == morseLetter[k]) 
       { 
        newMessage[j] = morseCode[k]; 
       } 
      }//end nested for loop 
     }//end for loop 
     return newMessage; 
    }//end method that completes translateion 

    public String toString(String a, String[] b) 
    { 
     String input = a; 
     String[] coded = b; 
     String[] encoded = new String[input.length()]; 
     //JOptionPane.showMessageDialog(null, "Original Text: " + input + "\nCoded Text: " + coded);  
     for(int l = 0; l <input.length(); l++) 
     { 
      encoded[l] = coded[l]; 
     } 
     String str = ""; 
     System.out.print(Arrays.toString(encoded)); 
     return str; 
    }//end toString method 
}//end Translate Class 

的莫爾斯電碼文本文件包含以下內容:

 
1 .---- 

2 ..--- 

3 ...-- 

4 ....- 

5 ..... 

6 -.... 

7 --... 

8 ---.. 

9 ----. 

0 ----- 

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 --.. 
+0

什麼應該您的toString消息實際上做?你只是從一個陣列到另一個陣列洗牌的字符串。 – RoflcoptrException 2011-04-03 21:41:06

+0

我的toString方法應該打印出原始消息加上它的莫爾斯碼版本,以及那些卡住的地方。 – Joey 2011-04-03 21:43:37

+0

當我添加如下內容後:'String str; \t \t \t \t str =「original text:」+ input +「\ nmorse code:」+ encoded; \t \t \t \t System.out.print(str); '它返回的內存地址,我需要的值 – Joey 2011-04-03 21:45:43

回答

2

Translate.toString方法將總是返回一個空字符串,因爲這是你分配給str變量的唯一的事。

+0

那麼我怎麼去實際打印存儲在數組newMessage []中的翻譯文本呢? – Joey 2011-04-03 21:36:05

+0

我會遍歷數組並像Roflcoptr的回答一樣構建一個字符串。不過,我會在調用方法而不是「toString」方法中打印結果。 – David 2011-04-03 23:05:56

0
public String toString(String a, String[] b) 
{ 
    System.out.println("Input: " + a); 
    System.out.println("Output:"); 

    String output = ""; 
    for(int i = 0; i < b.length; i++) 
    { 
     output = output + b[i]; 
    } 

    return output; 

}//end toString method 

但真正的問題是這樣的循環:

for(int i = 0; i < MAX; i++) 
     { 
      while(inFile.hasNext()) 
      { 
       line = inFile.next(); 
       code = (char)line.charAt(0); 
       //System.out.println(code); 
       morseLetter[i] = code; 
       morseCode[i] = inFile.next(); 
      }//end nested while loop 
     }//end for loop 

有嘗試解析莫爾斯電碼字母到一個文件,但問題是for循環在第一次迭代,然後,將整個while循環被執行,所以你總是把莫爾代碼放在數組的第一個位置。

所以應該看起來像:

int counter = 0; 
     while(inFile.hasNext()) 
      { 
       line = inFile.next(); 
       code = (char)line.charAt(0); 
       //System.out.println(code); 
       morseLetter[counter] = code; 
       morseCode[counter] = inFile.next(); 
       counter++; 
      }//end nested while loop 

作爲附加的註釋:你有沒有注意到,在用戶點擊了您的OptionsDialog取消,有一個空指針?適當的異常處理是任務,我想的一部分;)

所以我會寫這樣的循環:

do 
     { 
      userInput = JOptionPane.showInputDialog("Please enter what you wish to translte to Morse code (no punctuation)."); 
      if (userInput != null && !userInput.equals("")) { 
       String compare = userInput.toUpperCase(); 
       String[] codedText = new String[compare.length()]; 

       codedText = text.translateHere(compare); 
       text.toString(userInput, codedText); 
      } 
     }while(userInput != null && !userInput.equals(SENTINEL)); 
+0

改變我的代碼來模仿這個後,我得到一個空白空間輸出應該是。我也試過使用JOPtionPane,看起來我只是拿到了我的數組在內存中持有莫爾斯碼轉換的位置。 – Joey 2011-04-03 22:01:48

+0

哦,是的,答案中有一個小錯誤。我糾正了它。抱歉。 – 2011-04-03 22:08:07

+0

我的輸出使用如下:[Ljava.lang.String; @ 4b222f [null] 輸入:a 輸出:我很感激這樣的幫助!我知道這可能看起來像是浪費時間或者其他東西,但是我一直在研究這個問題>< – Joey 2011-04-03 22:17:06