Question:
translate sentence to morse code in java?
MmMm
2012-03-25 16:20:38 UTC
My program is supposed to translate a sentence "a million dollars" into morse code.

My attempt at it.

Not sure where to go from here.

public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Please enter a String for translation:");
String input = scan.nextLine();
scan.nextLine();

Switch (morseCode)
{
ase 1:
'a' = ".-";
break;
case 2:
'b' = "-...";
break;
case 3:
'c' = "-.-.";
break;
case 4:
'd' = "-..";
break;
case 5:
'e' = ".";
break;
case 6:
'f' = "..-.";
break;
case 7:
'g' = "--.";
break;
case 8:
'h' = "....";
break;
case 9:
'i' = "..";
break;
case 10:
'j' = ".---";
break;
case 11:
'k' = "-.-";
break;
case 12:
'l' = ".-..";
break;
case 13:
'm' = "--";
break;
case 14:
'n' = "-.";
break;
case 15:
'o' = "---";
break;
case 16:
'p' = ".--.";
break;
case 17:
'q' = "--.-";
break;
case 18:
'r' = ".-.";
break;
case 19:
's' = "...";
break;
case 20:
't' = "-";
break;
case 21:
'u' = "..-";
break;
case 22:
'v' = "...-";
break;
case 23:
'w' = ".--";
break;
case 24:
'x' = "-..-";
break;
case 25:
'y' = "-.--";
break;
case 26:
'z' = "--..";
break;
default:
System.out.println("Your input is invalid");
}


}
Five answers:
modulo_function
2012-03-25 16:58:05 UTC
That's a good start. I'll tryto help some more.



1) you need to include a case for the space between words.



You probably didn't try to compile this because

Switch(..)

is wrong, it's

switch(..)



Here's how I would do it:



declare a char array holding all the letters:

char[] ltr = {'a','b','c','d',..'z' ' '};

that last char is the space



Also define a string array to hold the converted values:

String[] morse = {".-", ..." "}

make sure that ltr and morse are the same length



once you get your string input then



for( int k=0; k
...for( int j=0; j
......if( input.charAt(k) == ltr[j] ) System.out.print(morse[j]);

...}

}



That should do it. Quite a bit simpler than all that case stuff, no? There are some refinements that could be made but code this up and come back with your code and I'll help some more.



+add

If you want to keep your switch logic then keep the outer k loop and replace what's inside with



String outStr;

switch(input.charAt(k)){

case 'a':

outStr = ".-";

break;

case 'b':

...etc

}

System.out.print( outStr );



I'll need to see your attempt at this before I offer any more help.
?
2016-10-04 16:46:36 UTC
Translate Morse Code
2015-08-13 11:38:23 UTC
This Site Might Help You.



RE:

translate sentence to morse code in java?

My program is supposed to translate a sentence "a million dollars" into morse code.



My attempt at it.



Not sure where to go from here.



public static void main(String[] args)

{

Scanner scan = new Scanner(System.in);

System.out.print("Please enter a...
Computer Technician
2012-03-25 16:33:29 UTC
Your Morse code looks okay. It's harder for me to read written down than to hear it. I was a Amateur Ham radio operator about 47 years ago. So I am a little rusty. I could send and receive 30 wpm. Now I might do 5 LOL.

--- -.- good luck
brilliant_moves
2012-03-25 18:24:36 UTC
import java.util.Scanner;



public class morse {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Please enter a String for translation:");

String input = scan.nextLine();

System.out.println( convertToMorse( input));

}



public static String convertToMorse(String s) {

String answer = "";

s = s.toLowerCase();

for (int i=0; i
char ch = s.charAt(i);

if (ch=='a') answer += ".-";

else if (ch=='b') answer += "-...";

else if (ch=='c') answer += "-.-.";

else if (ch=='d') answer += "-..";

else if (ch=='e') answer += ".";

else if (ch=='f') answer += "..-.";

else if (ch=='g') answer += "--.";

else if (ch=='h') answer += "....";

else if (ch=='i') answer += "..";

else if (ch=='j') answer += ".---";

else if (ch=='k') answer += "-.-";

else if (ch=='l') answer += ".-..";

else if (ch=='m') answer += "--";

else if (ch=='n') answer += "-.";

else if (ch=='o') answer += "---";

else if (ch=='p') answer += ".--.";

else if (ch=='q') answer += "--.-";

else if (ch=='r') answer += ".-.";

else if (ch=='s') answer += "...";

else if (ch=='t') answer += "-";

else if (ch=='u') answer += "..-";

else if (ch=='v') answer += "...-";

else if (ch=='w') answer += ".--";

else if (ch=='x') answer += "-..-";

else if (ch=='y') answer += "-.--";

else if (ch=='z') answer += "--..";

else if (ch==' ') answer += ""; // do nothing

else System.out.println("Your input is invalid");

answer += " "; // a space after each letter of morse code

}

return answer;

}

}


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