gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:55:07 26 4
gpt4 key购买 nike

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

我翻译消息的类。

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 结果:1

最佳答案

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

关于java - 摩尔斯电码,从英语到摩尔斯电码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15750405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com