Reputation:
I'm trying to make a converter from english alphabet letters to morse code, but I'm having difficulties when it comes to more than one inputed letter. I can tell what my problem is, but not the solution. I'm very new to java and I know the way I'm doing it is very sloppy and repetitive, but this is all I could figure out with what I know so far.
I made some scenarios such as, figure out how many letters are in the input, and convert each letter; but again, I'm not sure if this would work or if it's even possible.
import java.util.Scanner;
public class JavaPractice {
public static void main(String[] args) {
String morseA;
morseA = "•-";
String morseB;
morseB = "-•••";
String morseC;
morseC = "-•-•";
String morseD;
morseD = "-••";
String morseE;
morseE = "•";
String morseF;
morseF = "••-•";
String morseG;
morseG = "--•";
String morseH;
morseH = "••••";
String morseI;
morseI = "••";
String morseJ;
morseJ = "•---";
String morseK;
morseK = "-•-";
String morseL;
morseL = "•-••";
String morseM;
morseM = "--";
String morseN;
morseN = "-•";
String morseO;
morseO = "---";
String morseP;
morseP = "•--•";
String morseQ;
morseQ = "--•-";
String morseR;
morseR = "•-•";
String morseS;
morseS = "•••";
String morseT;
morseT = "-";
String morseU;
morseU = "••-";
String morseV;
morseV = "•••-";
String morseW;
morseW = "•--";
String morseX;
morseX = "-••-";
String morseY;
morseY = "-•--";
String morseZ;
morseZ = "--••";
Scanner morseInput = new Scanner(System.in);
System.out.println("type a letter and it will be converted into morse code!");
String morseTranslation = morseInput.nextLine();
if (morseTranslation.length() > 0) {
if (morseTranslation.equals("a")) {
System.out.println(morseA);
}
if (morseTranslation.equals("b")) {
System.out.println(morseB);
}
if (morseTranslation.equals("c")) {
System.out.println(morseC);
}
if (morseTranslation.equals("d")) {
System.out.println(morseD);
}
if (morseTranslation.equals("e")) {
System.out.println(morseE);
}
if (morseTranslation.equals("f")) {
System.out.println(morseF);
}
if (morseTranslation.equals("g")) {
System.out.println(morseG);
}
if (morseTranslation.equals("h")) {
System.out.println(morseH);
}
if (morseTranslation.equals("i")) {
System.out.println(morseI);
}
if (morseTranslation.equals("j")) {
System.out.println(morseJ);
}
if (morseTranslation.equals("k")) {
System.out.println(morseK);
}
if (morseTranslation.equals("l")) {
System.out.println(morseL);
}
if (morseTranslation.equals("m")) {
System.out.println(morseM);
}
if (morseTranslation.equals("n")) {
System.out.println(morseN);
}
if (morseTranslation.equals("o")) {
System.out.println(morseO);
}
if (morseTranslation.equals("p")) {
System.out.println(morseP);
}
if (morseTranslation.equals("q")) {
System.out.println(morseQ);
}
if (morseTranslation.equals("r")) {
System.out.println(morseR);
}
if (morseTranslation.equals("s")) {
System.out.println(morseS);
}
if (morseTranslation.equals("t")) {
System.out.println(morseT);
}
if (morseTranslation.equals("u")) {
System.out.println(morseU);
}
if (morseTranslation.equals("v")) {
System.out.println(morseV);
}
if (morseTranslation.equals("w")) {
System.out.println(morseW);
}
if (morseTranslation.equals("x")) {
System.out.println(morseX);
}
if (morseTranslation.equals("y")) {
System.out.println(morseY);
}
if (morseTranslation.equals("z")) {
System.out.println(morseZ);
}
System.out.println(morseTranslation.charAt(0) + " " + morseTranslation.charAt(1));
}
}
}
// if statements
// long but might work
// charAt find out how many letters they typed in/inputed
// make something that uses chatAt for each character depending on how many there are,
// and individually translate each letter
Upvotes: 0
Views: 161
Reputation: 497
I would recommend you to store each letter and their morse translations in two separate arrays or as key value pairs in a map. For example :
String [] morsetrans = {"•-","-•••", and so on};
String [] letters = {"a","b", and so on};
I think this would be a much easier way to do things. You just need to write the logic accordingly to match the letters in the array and take their corresponding morse translation from the array that corresponds to the morse translation using the index of the letter.
For "a" index is 0 and its corresponding more translation is located in the same index in morsetrans array.
Upvotes: 2
Reputation: 144
Here is a quick solution to your problem, note that there could be simpler/easier to read ways to store the morse code, but for now this will do:
String[] morseCode = {"•-","-•••","-•-•", "-••","•","••-•","--•", "••••", "••","•---","-•-","•-••", "--","-•","---", "•--•","--•-", "•-•", "•••", "-", "••-", "•••-", "•--","-••-", "-•--", "--••"};
String alphabet = "abcdefghijklmnopqrstuvwxyz";
Scanner morseInput = new Scanner(System.in);
System.out.println("type something and it will be converted into morse code!");
String morseTranslation = morseInput.nextLine().toLowerCase();
for(int i = 0; i < morseTranslation.length(); i++)
System.out.print(morseCode[alphabet.indexOf(morseTranslation.charAt(i))] + " ");
First, by using a loop, you can convert a string of any length. Also, by storing all your values in an array/enum or any type of collection, you can simply refer to a code by it's position to avoid repeating yourself (ie: 8th letter of the alphabet should match 8th morse code)
ps: morseInput.nextLine().toLowerCase()
is used to only have to compare
lowercase letters instead of implementing lowercases AND uppercase letters. If you want to save the message in its original form, you can always store it first before applying .toLowerCase()
Upvotes: 0
Reputation: 528
Because you are using letters, you could use charAt()
to convert a letter into a char
, which can then be easily turned into an index of an array by using the ASCII value of the char. You can store the morse letters as a separate array and easily convert from alphabetic characters to their morse representations like so:
import java.util.Scanner;
public class JavaPractice {
//You can store the morse alphabet as a constant array to
//group all the letters together into one data structure
public static final String[] MORSE_ALPHABET = new String[] {
"•-", "-•••", "-•-•", "-••", "•",
"••-•", "--•", "••••", "••", "•---",
"-•-", "•-••", "--", "-•", "---",
"•--•", "--•-", "•-•", "•••", "-",
"••-", "•••-", "•--", "-••-", "-•--", "--••"};
public static void main(String[] args) {
Scanner morseInput = new Scanner(System.in);
System.out.println("type a letter and it will be converted into morse code!");
String morseTranslation = morseInput.nextLine();
for (int i = 0; i < morseTranslation.length(); i++) {
System.out.print(alphaToMorse(morseTranslation.charAt(i)) + " ");
}
System.out.println();
}
public static String alphaToMorse(char c) {
if (c > 91) { //this would convert lowercase letters to their uppercase counterparts
c -= 32;
}
if (c < 91 && c > 64) { //65-90 are the ASCII representations for A-Z
return MORSE_ALPHABET[c - 65]; //A = 65 which maps to index 0, B = 66 maps to index 1, etc.
}
return "";
}
}
For input "Happy":
output: "•••• •- •--• •--• -•-- "
Upvotes: 0
Reputation: 1256
As you are a beginner in JAVA learn while
and for
loop to fix your problem. You have to loop through the String
input line to print morse code. Learn switch
case
also. I am not providing any code as you are beginner and may not have any knowledge about loops.
Upvotes: 0
Reputation: 54
Please try this edited code,It will print input word in Morse code
import java.util.Scanner;
public class JavaPractice {
public static void main(String[] args) {
String morseA;
morseA = "•-";
String morseB;
morseB = "-•••";
String morseC;
morseC = "-•-•";
String morseD;
morseD = "-••";
String morseE;
morseE = "•";
String morseF;
morseF = "••-•";
String morseG;
morseG = "--•";
String morseH;
morseH = "••••";
String morseI;
morseI = "••";
String morseJ;
morseJ = "•---";
String morseK;
morseK = "-•-";
String morseL;
morseL = "•-••";
String morseM;
morseM = "--";
String morseN;
morseN = "-•";
String morseO;
morseO = "---";
String morseP;
morseP = "•--•";
String morseQ;
morseQ = "--•-";
String morseR;
morseR = "•-•";
String morseS;
morseS = "•••";
String morseT;
morseT = "-";
String morseU;
morseU = "••-";
String morseV;
morseV = "•••-";
String morseW;
morseW = "•--";
String morseX;
morseX = "-••-";
String morseY;
morseY = "-•--";
String morseZ;
morseZ = "--••";
Scanner morseInput = new Scanner(System.in);
System.out.println("type a letter and it will be converted into morse code!");
String morseTranslation = morseInput.nextLine();
if (morseTranslation.length() > 0) {
for (int i = 0; i < morseTranslation.length(); i++) {
String character = ""+morseTranslation.charAt(i);
if (character.contains("a")) {
System.out.println(morseA);
}
if (character.contains("b")) {
System.out.println(morseB);
}
if (character.contains("c")) {
System.out.println(morseC);
}
if (character.contains("d")) {
System.out.println(morseD);
}
if (character.contains("e")) {
System.out.println(morseE);
}
if (character.contains("f")) {
System.out.println(morseF);
}
if (character.contains("g")) {
System.out.println(morseG);
}
if (character.contains("h")) {
System.out.println(morseH);
}
if (character.contains("i")) {
System.out.println(morseI);
}
if (character.contains("j")) {
System.out.println(morseJ);
}
if (character.contains("k")) {
System.out.println(morseK);
}
if (character.contains("l")) {
System.out.println(morseL);
}
if (character.contains("m")) {
System.out.println(morseM);
}
if (character.contains("n")) {
System.out.println(morseN);
}
if (character.contains("o")) {
System.out.println(morseO);
}
if (character.contains("p")) {
System.out.println(morseP);
}
if (character.contains("q")) {
System.out.println(morseQ);
}
if (character.contains("r")) {
System.out.println(morseR);
}
if (character.contains("s")) {
System.out.println(morseS);
}
if (character.contains("t")) {
System.out.println(morseT);
}
if (character.contains("u")) {
System.out.println(morseU);
}
if (character.contains("v")) {
System.out.println(morseV);
}
if (character.contains("w")) {
System.out.println(morseW);
}
if (character.contains("x")) {
System.out.println(morseX);
}
if (character.contains("y")) {
System.out.println(morseY);
}
if (character.contains("z")) {
System.out.println(morseZ);
}
}
System.out.println(morseTranslation.charAt(0) + " " + morseTranslation.charAt(1));
}
}
}
Upvotes: 0