2009-12-06 5 views
6

私はモールス符号をテキストに変換しようとしています。Java:MorseCode Converter

この問題で使用する2つのテキストファイルがあります。 morseCode.txt:文字とそれに対応するモールス符号に相当するファイルがあります。

morse.dat:そのモールスコードで暗号化されたメッセージを含むファイルIが適切に最初のファイルを読み込み、次いで別々のアレイに次に保存することができた

。私は文字と配列モールスのコードの配列を印刷してそれをテストし、それが正しく、順番に格納していたことがわかりました。

メッセージの2番目のファイルを読むことに問題があります。 これはmorseCode.txtキーです:ouputをすることになって

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

その::これはMessage.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 ----. 
. .-.-.- 
, --..-- 
? ..--.. 

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

+0

メソッドBTWの使い方は教えていません。私のコードがなぜこのようなものなのか不思議に思う人もいます。 – HelloWorld

答えて

3

2つのタイプの区切り文字(スペースと改行)があるため、Scannerを使用してmorse.datファイルを解析する際に問題が発生します。一番簡単な方法は、おそらくBufferedReaderを使って一度に1行を読み込み、その行をスペースに分割することです。これを行うコードは次のとおりです。あなたはまだそれらについて学んでいないので、私は方法を使うことを避けました。

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としてファイルを開いて、実際にそれで何もしないを考えると、私はそれが動作していない驚いていません。

データファイルから個々のトークンを取得し、モールス配列でインデックスを参照したいとします。