Question:
create java program to translate morse code?
2012-09-09 13:26:13 UTC
Can someone help me with this? I need to develop a program that will translate English message files to Morse code, and Morse code message files to English. The program needs to be able to open user specified file with file extension “.eng” or “.mor” and must save the translation the appropriate file .mor or .eng! The translation should appear on screen. Each letter is on it’s own line, skipped line indicates end of word, 2 skipped lines for end of sentence. I need to use Scanner to read files. do i need to set up string arrays to read the file? This is what i have so far...

public static void main(String args[])
{
String fileName;
String morseA = ".-", morseB = "-...", morseC = "-.-.", morseD = "-..", morseE = ".",
morseF = "..-.", morseG = "--.", morseH = "....", morseI = "..", morseJ = ".---",
morseK = "-.-", morseL = ".-..", morseM = "--", morseN = "-.", morseO = "---",
morseP = ".--.", morseQ = "--.-", morseR = ".-.", morseS = "...", morseT = "-",
morseU = "..-", morseV = "...-", morseW = ".--", morseX = "-..-", morseZ = "--..",
morse1 = ".----", morse2 = "..---", morse3 = "...--", morse4 = "....-",
morse5 = ".....", morse6 = "-....", morse7 = "--...", morse8 = "---..",
morse9 = "----.", morse0 = "-----", morseCom = "--..--", morseQu = "..--..",
morsePeriod = ".-.-.-";




int choice = 0;
Scanner input = new Scanner(System.in);
System.out.println("Choose (1) to translate your english file to morse code or (2) morse code to english?");
choice = input.nextInt();

System.out.println("What is the name of the file? ");
fileName = input.next();


try
{
if (choice == 1)
{
Scanner fin = new Scanner(new File("C:\\" + fileName + ".txt"));

System.out.println(fileName +".txt is opened! Translation to morse code is in progress!" );

int c = 0;
while (fin.hasNext())
{

String txt = fin.nextLine();
System.out.print(txt);
if( txt.equals('H') || txt.equals('h') )
{
System.out.println(morseH);
}



}

} // end translation to morse code
else if (choice == 2)
{
Scanner fin = new Scanner(new File("C:\\" + fileName + ".mor"));



}


}

catch (Exception e)
{
System.out.println("Problems reading the file");
e.printStackTrace();
}


} // close main


} //end

Can someone tell me what i should add or change because i don't think i am doing it right?
Three answers:
husoski
2012-09-09 14:54:08 UTC
I don't think any change is going to improve the program much, but you can get it working. You just need a whole bootload of if/else statements. If you are using JDK7, you can also use switch/case with strings, now. That's brand new, and wont' work on JDK6 and earlier.



Does your input file really have one letter per line? That's what you're doing when you do a fin.nextLine() and compare the whole result to a character. I suspect each line is indeed a line with multiple characters to translate. One way to loop through the characters of a string is to use "for-each":



String txtLine = fin.nextLine();

for (char txtChar : txtLine)

{

... translate txtChar to morse here

}



To do the actual translation, the way you have set up the translation data, you can use an if/else if/else ladder:



if (txtChar == 'a' || txtChar == 'A')

{

result += " " + morseA ;

}

else if (txtchar == 'b' || txtChar == 'B')

{

result += " " + morseB;

}

else if (txtchar == 'c' || txtChar == 'C')

{

result += " " + morseC;

}

... add an "else if" block for each character you want to translate.



You should also have an else at the end to catch any unsupported characters:



else

{

result += " ?" + txtChar + "?";

}



Remember to add a code for a space, and output an extra blank space or two to represent the extra delay between words.



A more flexible approach would be create two ArrayList objects, add the English characters to one and the Morese equivalent to the other. Then search one list for the code in one form, and the same index in the other list gets you the translation. That works both ways, and ArrayList has an indexOf() method that will do the searching for you.
?
2016-10-17 04:04:13 UTC
Morse Code To English Translator
2016-02-22 03:04:31 UTC
Your string parsing logic is flawed. The way you are looking at the strings it is possible for multiple conditions to be true. You didn't indicate exactly where the problem was but perhaps fixing the logic errors in your parsing will fix the other error as well.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...