摩尔斯电码不是 Working/Running
Morse Code Not Working/Running
尝试 运行 我的代码时总是出现以下错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at MorseCodeToPlainText.main(MorseCodeToPlainText.java:13)
这是我的代码:
public class MorseCodeToPlainText {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
StringBuilder sb = new StringBuilder`enter code here`();
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
String fileAsString = sb.toString();
System.out.println(fileAsString);
String []morseCodes = fileAsString.split(" | ");
String plainText = "";
for(int i=0; i<morseCodes.length; i++)
{
plainText += decode(morseCodes[i]);
}
System.out.println(plainText);
writeDataToFile("decoded.txt", plainText);
}
public static String decode (String mCode) {
String pCode = " ";
if (mCode.equals(".-"))
pCode = "A";
else if (mCode.equals("-..."))
pCode = "B";
else if (mCode.equals("-.-."))
pCode = "C";
else if (mCode.equals("-.."))
pCode = "D";
else if (mCode.equals("."))
pCode = "E";
else if (mCode.equals("..-."))
pCode = "F";
else if (mCode.equals("--."))
pCode = "G";
else if (mCode.equals("...."))
pCode = "H";
else if (mCode.equals(".."))
pCode = "I";
else if (mCode.equals(".---"))
pCode = "J";
else if (mCode.equals("-.-"))
pCode = "K";
else if (mCode.equals(".-.."))
pCode = "L";
else if (mCode.equals("--"))
pCode = "M";
else if (mCode.equals("-."))
pCode = "N";
else if (mCode.equals("---"))
pCode = "O";
else if (mCode.equals(".--."))
pCode = "P";
else if (mCode.equals("--.-"))
pCode = "Q";
else if (mCode.equals(".-."))
pCode = "R";
else if (mCode.equals("..."))
pCode = "S";
else if (mCode.equals("-"))
pCode = "T";
else if (mCode.equals("..-"))
pCode = "U";
else if (mCode.equals("...-"))
pCode = "V";
else if (mCode.equals(".--"))
pCode = "W";
else if (mCode.equals("-..-"))
pCode = "X";
else if (mCode.equals("-.--"))
pCode = "Y";
else if (mCode.equals("--.."))
pCode = "Z";
else if (mCode.equals("-----"))
pCode = "0";
else if (mCode.equals(".----"))
pCode = "1";
else if (mCode.equals("..---"))
pCode = "2";
else if (mCode.equals("...--"))
pCode = "3";
else if (mCode.equals("....-"))
pCode = "4";
else if (mCode.equals("....."))
pCode = "5";
else if (mCode.equals("-...."))
pCode = "6";
else if (mCode.equals("--..."))
pCode = "7";
else if (mCode.equals("---.."))
pCode = "8";
else if (mCode.equals("----."))
pCode = "9";
else if (mCode.equals(".-.-.-"))
pCode = ".";
else if (mCode.equals("--..--"))
pCode = ",";
else if (mCode.equals("-..-."))
pCode = "/";
else if (mCode.equals("..--.."))
pCode = "?";
return pCode;
}
public static void writeDataToFile(String fileName, String fileText){
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
pw.println(fileText);
pw.flush();
pw.close();
bw.close();
fw.close();
}
catch (IOException io) {
System.out.println("Problem ocurred during file operation...");
}
}
}
您正在使用第一个参数构建 BufferedReader
.args[0]。
BufferedReader br = new BufferedReader(new FileReader(args[0]));
当您 运行 这个程序时,您应该将文件路径作为命令行参数传递。
还将 StringBuilder 声明更正为 StringBuilder sb = new StringBuilder("enter code here");
如果您询问如何在 Eclipse 中传递命令行参数。这张截图应该会有帮助。
尝试 运行 我的代码时总是出现以下错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at MorseCodeToPlainText.main(MorseCodeToPlainText.java:13)
这是我的代码:
public class MorseCodeToPlainText {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(args[0]));
StringBuilder sb = new StringBuilder`enter code here`();
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
String fileAsString = sb.toString();
System.out.println(fileAsString);
String []morseCodes = fileAsString.split(" | ");
String plainText = "";
for(int i=0; i<morseCodes.length; i++)
{
plainText += decode(morseCodes[i]);
}
System.out.println(plainText);
writeDataToFile("decoded.txt", plainText);
}
public static String decode (String mCode) {
String pCode = " ";
if (mCode.equals(".-"))
pCode = "A";
else if (mCode.equals("-..."))
pCode = "B";
else if (mCode.equals("-.-."))
pCode = "C";
else if (mCode.equals("-.."))
pCode = "D";
else if (mCode.equals("."))
pCode = "E";
else if (mCode.equals("..-."))
pCode = "F";
else if (mCode.equals("--."))
pCode = "G";
else if (mCode.equals("...."))
pCode = "H";
else if (mCode.equals(".."))
pCode = "I";
else if (mCode.equals(".---"))
pCode = "J";
else if (mCode.equals("-.-"))
pCode = "K";
else if (mCode.equals(".-.."))
pCode = "L";
else if (mCode.equals("--"))
pCode = "M";
else if (mCode.equals("-."))
pCode = "N";
else if (mCode.equals("---"))
pCode = "O";
else if (mCode.equals(".--."))
pCode = "P";
else if (mCode.equals("--.-"))
pCode = "Q";
else if (mCode.equals(".-."))
pCode = "R";
else if (mCode.equals("..."))
pCode = "S";
else if (mCode.equals("-"))
pCode = "T";
else if (mCode.equals("..-"))
pCode = "U";
else if (mCode.equals("...-"))
pCode = "V";
else if (mCode.equals(".--"))
pCode = "W";
else if (mCode.equals("-..-"))
pCode = "X";
else if (mCode.equals("-.--"))
pCode = "Y";
else if (mCode.equals("--.."))
pCode = "Z";
else if (mCode.equals("-----"))
pCode = "0";
else if (mCode.equals(".----"))
pCode = "1";
else if (mCode.equals("..---"))
pCode = "2";
else if (mCode.equals("...--"))
pCode = "3";
else if (mCode.equals("....-"))
pCode = "4";
else if (mCode.equals("....."))
pCode = "5";
else if (mCode.equals("-...."))
pCode = "6";
else if (mCode.equals("--..."))
pCode = "7";
else if (mCode.equals("---.."))
pCode = "8";
else if (mCode.equals("----."))
pCode = "9";
else if (mCode.equals(".-.-.-"))
pCode = ".";
else if (mCode.equals("--..--"))
pCode = ",";
else if (mCode.equals("-..-."))
pCode = "/";
else if (mCode.equals("..--.."))
pCode = "?";
return pCode;
}
public static void writeDataToFile(String fileName, String fileText){
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null;
try {
fw = new FileWriter(fileName);
bw = new BufferedWriter(fw);
pw = new PrintWriter(bw);
pw.println(fileText);
pw.flush();
pw.close();
bw.close();
fw.close();
}
catch (IOException io) {
System.out.println("Problem ocurred during file operation...");
}
}
}
您正在使用第一个参数构建 BufferedReader
.args[0]。
BufferedReader br = new BufferedReader(new FileReader(args[0]));
当您 运行 这个程序时,您应该将文件路径作为命令行参数传递。
还将 StringBuilder 声明更正为 StringBuilder sb = new StringBuilder("enter code here");
如果您询问如何在 Eclipse 中传递命令行参数。这张截图应该会有帮助。