2009-12-06 45 views
6

我試圖將莫爾斯電碼轉換爲文本。Java:MorseCode轉換器

我有兩個文本文件用於此問題。 morseCode.txt:我有一個文件,其中包含字母和相應的莫爾斯碼等價物。

morse.dat:其包含在莫爾斯電碼的加密的消息的文件

我能夠正確讀取的第一個文件,然後再存儲到單獨的陣列。我通過打印陣列中的字母和數組莫爾斯碼來測試它,發現它確實按順序存儲了它。

我在閱讀第二個文件的消息時遇到了問題。 這是morseCode.txt關鍵:

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 ----- 
1 .---- 
2 ..--- 
3 ...-- 
4 ....- 
5 ..... 
6 -.... 
7 --... 
8 ---.. 
9 ----. 
. .-.-.- 
, --..-- 
? ..--.. 

這是Message.txt文件的樣子:

- .... .. ... 
.. ... 
.- 
- . ... - 
.--. .-. --- --. .-. .- -- .-.-.- 
.. ..-. 
-.-- --- ..- 
... . . 
- .... .. ... 
-- . ... ... .- --. . 
- .... .- - 
.. ... 
--. --- --- -.. 
-. . .-- ... 
-.-- --- ..- .-. 
.--. .-. --- --. .-. .- -- 
.-- --- .-. -.- ... .-.-.- 

它應該輸出繼電器:

THIS IS A TEST PROGRAM. 
IF YOU SEE THIS MESSAGE THAT IS GOOD NEWS YOUR PROGRAM WORKS. 

+++這是我的解決方案+++

/*  
*This is a program that translates Morse Code to English text. The key is read from a file. 
* 
*The file contains the equivalent letters and Morse code. The entire key is stored in an array for reference. 
* 
*A second file contains the encrypted message. The program reads the message file and converts individual Morse code 
* 
*letters into alphabet letters. The results are displayed and printed out to another file. */ 

import java.io.*; 
import java.util.Scanner; 

//file writer and print writer to save the output 
public class Assignment4 
{ 
@SuppressWarnings("null") 
public static void main (String [] args) throws IOException 
{ 
    //arrays for letters and Morse Code values 
    String[] letter = new String[39], morseCode = new String[39];  
    //this is the file with the letters and morse code equivalent 
    File file1 = new File ("c:/morseCode.txt"); 
    Scanner in = new Scanner(file1); 

    int i = 0; 

    while (in.hasNext()) 
     { 
      //read in letter 
    letter[i] = in.next(); 
    //read in Morse Code 
      morseCode[i] = in.next();   

      i++; 
     } 

    //this is the file with encrypted message 
    String morseLine, morseLetter, theLetter = " "; 
    //this file contains the encrypted message 
    File file2 = new File ("c:/morse.dat"); 
    Scanner data = new Scanner(file2); 

    //this appends the data to the file and keeps a running input 
    FileWriter fWriter = new FileWriter("c:/Message.txt", true); 
    PrintWriter outPutFile = new PrintWriter(fWriter); 

    boolean found; 
    int number = morseCode.length; 

    while (data.hasNext()) 
    { 
    //reads each line of mesage 
    morseLine = data.nextLine(); 

    i = 0; 
    int j = 0, k = 0;  
    j = morseLine.indexOf(" "); 

    while (j != -1) 
    { 
    //determines the end of a letter in Morse code and stores the 
    morseLetter = morseLine.substring(i, j); 

    found = false;  
    k = 0;  
    theLetter = " "; 

    //this loop searches the array of morseCode for the equal Morse code letter 
    //then it assigns the corresponding letter that is equivalent 
    while (k < number && !found) 
    { 
    if (morseLetter.equals(morseCode[k])) 
    { 
     theLetter = letter[k];  
     found = true; 
    } 

    else 
    { 
     k++; 
    } 
    } 

    //this condition prints the equivalent letter of the Morse code letter 
    //on the message 
    if (!theLetter.equals(" ")) 
    { 
    System.out.print(theLetter);  
    outPutFile.print(theLetter); 
    } 

    i = j + 1;  
    j = morseLine.indexOf(" ", i);  
    } 

    if(theLetter.equals(".")) 
    { 
    //moves to the next line at the end of the sentence 
    System.out.println(""); 
    outPutFile.println(""); 
    } 

    else 
    { 
    //this separates the letters into words 
    System.out.print(" "); 
    outPutFile.print(" "); 
    } 
    } 

    outPutFile.close(); 
    } 
} 
+0

嗨馬克, 我無法按照您的解決方案,但我確實想出了一個解決方案。自從上課以來,我們在課程中沒有進入面向對象編程。您能否使用Buffered Reader向我展示問題的完整解決方案。我把我的解決方案放在最上面。謝謝。 – HelloWorld 2009-12-12 22:38:57

+0

我們沒有被教過如何使用方法BTW。有些人一直在想爲什麼我的代碼是這樣的。 – HelloWorld 2009-12-12 22:43:14

回答

3

使用掃描儀解析morse.dat文件時會出現問題,因爲有兩種類型的分隔符 - 空格和換行符。最簡單的方法可能是使用BufferedReader一次讀取一行,然後將行分割到一個空格上。這是執行該操作的代碼。我避免使用方法,因爲你還沒有了解它們。

import java.io.*; 
import java.util.*; 

public class Assignment4 
{ 
    public static void main(String[] args) throws IOException 
    { 
     Map<String, String> morseCodes = new HashMap<String, String>(); 
     File file1 = new File ("morsecode.txt"); 
     Scanner in = new Scanner(file1); 

     while (in.hasNext()) 
     { 
      String letter = in.next();   
      String code = in.next(); 
      morseCodes.put(code, letter); 
     } 

     File morseCode = new File("message.txt"); 
     BufferedReader bufferedReader = new BufferedReader(new FileReader(morseCode)); 
     String line; 

     while ((line = bufferedReader.readLine()) != null) 
     { 
      String letter = ""; 

      for (String morseLetter: line.split(" ")) 
      { 
       letter = morseCodes.get(morseLetter); 
       System.out.print(letter); 
      } 

      if (letter.equals(".")) { 
       // Insert a new line after a period. 
       System.out.println(); 
      } else { 
       // Insert a space between words. 
       System.out.print(' '); 
      } 
     } 
    } 
} 

輸出上述程序的:

THIS IS A TEST PROGRAM. 
IF YOU SEE THIS MESSAGE THAT IS GOOD NEWS YOUR PROGRAM WORKS. 

我希望可以幫助你!

2
while (data.hasNext()) 
{ 
    //read in letter 
      words[e] = message[e].indexOf(" "); 

考慮到你打開該文件作爲data和實際上並不用它做任何事情,我並不感到驚訝它不工作。

您想從數據文件中獲取單個令牌,然後在您的摩爾斯數組中查找它們的索引。