2009-12-06 13 views
6

मैं मोर्स कोड को टेक्स्ट में बदलने की कोशिश कर रहा हूं।जावा: मोर्सकोड कनवर्टर

मेरे पास इस समस्या के साथ उपयोग करने के लिए दो टेक्स्ट फ़ाइलें हैं। morseCode.txt: मेरे पास एक फ़ाइल है जिसे मैंने पढ़ा है जिसमें अक्षरों और संबंधित मोर्स कोड समतुल्य हैं।

morse.dat: इसका फ़ाइल कि मोर्स कोड में एन्क्रिप्टेड संदेश शामिल

मैं पहली बार फ़ाइल ठीक से पढ़ने के लिए और उसके बाद अलग विन्यास में, तो स्टोर में सक्षम था। मैंने अक्षरों और सरणी मोर्स कोड के लिए सरणी मुद्रित करके इसका परीक्षण किया और पाया कि यह इसे ठीक से और क्रम में संग्रहीत करता है।

मुझे दूसरी फ़ाइल संदेश पढ़ने में समस्याएं आ रही हैं।

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

इसके ouput करने वाला:

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 फ़ाइल लग रहा है: यह morseCode.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

हाय मार्क, मैं आपके समाधान का पालन नहीं कर सका लेकिन मैं एक समाधान के साथ आया था। हम अपने पाठ्यक्रम में ऑब्जेक्ट उन्मुख प्रोग्रामिंग में शुरुआत नहीं कर रहे हैं क्योंकि यह एक स्टार्टर क्लास है। क्या आप मुझे अपने बुफर्ड रीडर का उपयोग करके समस्या का पूरा समाधान दिखा सकते हैं। मैंने अपना समाधान ऊपर ऊपर पोस्ट किया। धन्यवाद। – HelloWorld

+0

हमें सिखाया नहीं गया है कि तरीके बीटीडब्ल्यू का उपयोग कैसे करें। कुछ लोग सोच रहे हैं कि मेरा कोड ऐसा क्यों है। – HelloWorld

उत्तर

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 के रूप में फ़ाइल खोलने के लिए और वास्तव में इसके साथ कुछ भी नहीं करते ध्यान में रखते हुए मैं यह काम नहीं कर रहा हैरान नहीं हूँ।

आप डेटा फ़ाइल से अलग टोकन प्राप्त करना चाहते हैं, और फिर अपने सूचकांक को अपने मोर्स सरणी में देखना चाहते हैं।

संबंधित मुद्दे