帮助摩尔斯电码Java分配!

时间:2011-04-03 21:13:03

标签: java

摩尔斯电码代码翻译的价值不断回归null,我已经做了我能想到的一切来修复它。我们被限制在此分配上使用数组。我该怎么做才能纠正它?

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

2 个答案:

答案 0 :(得分:2)

您的Translate.toString方法将始终返回一个空字符串,因为这是您分配给str变量的唯一内容。

答案 1 :(得分: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循环被执行,因此你总是将morsecode放在第一个位置数组。

所以看起来应该是这样的:

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中单击取消,则表示存在NullPointer?正确的异常处理是我认为的任务的一部分;)

因此我会像这样编写循环:

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));