morse java_Java:MorseCode Converter

我正在尝试将莫尔斯代码转换为文本.

我有两个文本文件用于此问题.

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值