摩尔斯电码,从英语到摩尔斯电码

时间:2013-04-01 19:34:36

标签: java

我创建了一个程序,将输入转换为摩尔斯电码并将其显示为输出。这是我的计划。

我翻译消息的课程。

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class MorseCode {
 public MorseCode()
    {
    }//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 = "morsecode.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);
        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
        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)
{
   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
}//end Translate Class

然后这是我测试的地方。

package morse.code;
import javax.swing.JOptionPane;
import java.io.*;
public class MorseCodeTester
{
    public static void main(String[] args)throws IOException
    {
        String userInput;
        final String SENTINEL = "0";//for exiting program when entered
        //object creation
        MorseCode text = new MorseCode();
        //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

这里的一切似乎都很好但是在我输入输入后它显示了这个错误。

 Exception in thread "main" java.io.FileNotFoundException: morsecode.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.util.Scanner.<init>(Scanner.java:656)
at morse.code.MorseCode.translateHere(MorseCode.java:30)
at morse.code.MorseCodeTester.main(MorseCodeTester.java:23)

Java Result: 1

2 个答案:

答案 0 :(得分:3)

如果您阅读documentation,您就会知道当JVM找不到文件时会抛出此异常。我怀疑您需要更新String line, file = "morsecode.txt"; //以准确反映morsecode.txt文件的位置。

答案 1 :(得分:0)

如果你需要,我有一个莫尔斯电码转换器。它使点成为 - 并且破灭 - 但如果你愿意,你可以改变它。

import java.util.Scanner;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("Enter Text For Conversion to Morse Code");

        Scanner scan = new Scanner(System.in);
        String s = scan.nextLine();
        System.out.println(print(toMorse(s)));
    }

    static boolean[] toMorse(String input) {
        boolean output[] = new boolean[0];
        for (int i = 0; i < input.length(); i++) { // Every character
            char onChar = input.charAt(i);
            output = addCharacter(output, onChar);
        }
        return output;

    }

    static boolean[] addCharacter(boolean[] input, char character) {
        character = Character.toLowerCase(character);
        if (character == 'a') {
            input = dot(input);
            input = dash(input);
        }
        if (character == 'b') {
            input = dash(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == 'c') {
            input = dash(input);
            input = dot(input);
            input = dash(input);
            input = dot(input);
        }
        if (character == 'd') {
            input = dash(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == 'e') {
            input = dot(input);
        }
        if (character == 'f') {
            input = dot(input);
            input = dot(input);
            input = dash(input);
            input = dot(input);
        }
        if (character == 'g') {
            input = dash(input);
            input = dash(input);
            input = dot(input);
        }
        if (character == 'h') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == 'i') {
            input = dot(input);
            input = dot(input);
        }
        if (character == 'j') {
            input = dot(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);
        }
        if (character == 'k') {
            input = dash(input);
            input = dot(input);
            input = dash(input);
        }
        if (character == 'l') {
            input = dot(input);
            input = dash(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == 'm') {
            input = dash(input);
            input = dash(input);
        }
        if (character == 'n') {
            input = dash(input);
            input = dot(input);
        }
        if (character == 'o') {
            input = dash(input);
            input = dash(input);
            input = dash(input);
        }
        if (character == 'p') {
            input = dot(input);
            input = dash(input);
            input = dash(input);
            input = dot(input);
        }
        if (character == 'q') {
            input = dash(input);
            input = dash(input);
            input = dot(input);
            input = dash(input);
        }
        if (character == 'r') {
            input = dot(input);
            input = dash(input);
            input = dot(input);
        }
        if (character == 's') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == 't') {
            input = dash(input);
        }
        if (character == 'u') {
            input = dot(input);
            input = dot(input);
            input = dash(input);
        }
        if (character == 'v') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dash(input);
        }
        if (character == 'w') {
            input = dot(input);
            input = dash(input);
            input = dash(input);
        }
        if (character == 'x') {
            input = dash(input);
            input = dot(input);
            input = dot(input);
            input = dash(input);
        }
        if (character == 'y') {
            input = dash(input);
            input = dot(input);
            input = dash(input);
            input = dash(input);

        }
        if (character == 'z') {
            input = dash(input);
            input = dash(input);
            input = dot(input);
            input = dot(input);
        }
        if (character == ' ') {
            input = add(input, false);
            input = add(input, false);
            input = add(input, false);
            input = add(input, false);
        }
        if (character == '1') {
            input = dot(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);

        }
        if (character == '2') {
            input = dot(input);
            input = dot(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);

        }
        if (character == '3') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dash(input);
            input = dash(input);

        }
        if (character == '4') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dash(input);

        }
        if (character == '5') {
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);

        }
        if (character == '6') {
            input = dash(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);

        }
        if (character == '7') {
            input = dash(input);
            input = dash(input);
            input = dot(input);
            input = dot(input);
            input = dot(input);

        }
        if (character == '8') {
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dot(input);
            input = dot(input);

        }
        if (character == '9') {
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dot(input);

        }
        if (character == '0') {
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);
            input = dash(input);
        }

        return input;
    }

    static boolean[] dot(boolean[] input) {
        input = add(input, true);
        input = add(input, false);
        return input;
    }

    static boolean[] dash(boolean[] input) {
        input = add(input, true);
        input = add(input, true);
        input = add(input, true);
        input = add(input, false);
        return input;
    }

    static boolean[] add(boolean[] input, boolean add) {
        int length = input.length;

        boolean[] newArray = new boolean[length + 1];
        for (int i = 0; i < length; i++) {
            newArray[i] = input[i];

        }
        newArray[length] = add;

        return newArray;

    }

    static String print(boolean[] input) {
        String output = "";
        for (int i = 0; i < input.length; i++) {
            if (input[i]) {
                output = output + "-";
            }
            if (!input[i]) {
                output = output + " ";
            }

        }

        return output;
    }

}