みんな。モールス符号に問題があります。「A」のような英語のメッセージまたはテキストをモールス符号「.-」に変換しようとしています。
ただし、convert メソッドに問題があります。コードを実行すると、見つかったメッセージが返されますが、モールス信号全体も読み取られます。
モールス符号の値を出力できるように、インデックスを比較する必要があると先生に言われました。しかし、私はまだロジックにこだわっています。
これが私がこれまでに得たものです:
public class MorseCode {
public static ArrayList<String> alpha;
public static ArrayList<String> codes;
/**
* This Constructor actually imports the list of Morse Code Characters and adds it to an ArrayList.
*/
public MorseCode() throws IOException
{
String token = "";
Scanner inFile = new Scanner(new File("morsecode.txt"));
codes = new ArrayList<String>();
codes.add(".-");
codes.add("-...");
codes.add("-.-.");
while(inFile.hasNext())
{
token = inFile.nextLine();
System.out.println(token);
}
inFile.close();
}
public static void alpha () throws IOException
{
Scanner inFile = new Scanner(new File("alphabet.txt"));
String cake = "";
alpha = new ArrayList<String>();
alpha.add("A");
alpha.add("B");
alpha.add("C");
while(inFile.hasNext())
{
cake = inFile.nextLine();
System.out.println(cake);
}
inFile.close();
}
public static ArrayList<String> getMorse()//ArrayList<String> codes
{
ArrayList codes = new ArrayList<String>();
return codes;
}
public static ArrayList<String> getAlpha()//ArrayList<String> alpha
{
ArrayList alpha = new ArrayList<String>();
return alpha;
}
public static void Morse ( String token)
{
for( String m : codes)
{
token = m;
System.out.println(m);
}
}
public static void Alpha(String cake)
{
for( String a : alpha)
{
cake = a;
System.out.println(a);
}
}
public static String convertCode (String myMessage)
{
alpha = new ArrayList<String>();
String myLetter = "";
String cake = "";
int myindex = 0;
for(int index = 0; index < myMessage.length(); index++)
{
myLetter = myMessage.substring(index,index + 1);
for( int in = 0; in < alpha.size(); in++)
{
if(alpha.get(in).equals(myLetter))
{
index = in;
}
cake += codes.get(in);
System.out.println(cake);
}
}
return cake;
}
}