2013-04-01 291 views
0

我創建了一個程序,將輸入轉換成莫爾斯電碼並顯示爲輸出。這是我的程序。莫爾斯電碼,從英文到莫爾斯電碼

我翻譯消息的類。

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

+3

'morsecode.txt'在哪裏?我希望你的程序找不到它。要測試這個,請指定完整路徑 –

回答

3

如果你讀了documentation你就會知道,當一個文件不能被JVM發現這個異常被拋出。我懷疑你需要更新String line, file = "morsecode.txt"; //以準確反映morsecode.txt文件的位置。

+0

是的,我把正確的文件,並仍然有相同的問題 – user2059140

+0

你確定你沒有任何區分大小寫的問題。如@MartinWilson建議的那樣,指定文件的絕對路徑並查看它是否仍然可以找到它。 – christopher

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

}