Hi I'm trying to make a java program that translates English to Morse and vice versa. Basically I'm having trouble getting everything to become compatible.. and how to use the replace all method to get all the varaiubles entered to be replaced by those corresponding i the index of Morse. I'm not trying to shortcut this much..I've really tried hard and want to get this done with. thank you so much!
import java.util.Scanner;
public class ProjMorse
{
public static void main( String [] args )
{
Scanner input = new Scanner(System.in);
String [] alpha = {"a",b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0"," "};
String [] dottie = {".-", "-...", "-.-.", "-..", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", "-----", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "|"};
System.out.println("To convert from English to Morse enter English");
String ans = input.nextLine();
if(ans.equals("English"))
{
System.out.println( "Please enter the text you would like to convert to Morse Code: ");
String English = input.nextLine();
char[] translates = (English.toLowerCase()).toCharArray();
for (int i = 0; i < alpha.length; i++)
{
String s = translates[i].replaceAll('i', (dottie[i]));
}
String s = new String(dottie[i]);
System.out.println(s);
}
else
{
System.out.println( "Please enter the text you would like to convert to English (separate words with '|'):");
String code = input.nextLine();
String[] translates = code.split("[|]", 0);
for (int j = 0; j < dottie.length; j++)
{
alpha[j] = String.valueOf(translates[j]);
}
String s = new String(alpha[j]);
System.out.println(s);
}
}
}
This wont work, what you are trying to do is step through each character of the string to be translated (which you have converted to a char array) and do a
replaceAll
instances of the letter 'i' with the relative index in the morse code alphabet.replaceAll
is a method of the String class, this wont work.If you hadn't converted it to a char array you could do a
replaceAll
on the plaintext string containing chars inalpha
with each item indottie
.Additionally...
First, if you are asking a question, it should contain a question, with question mark and all. So that we know what we're answering. If you have an error you need to include what error that is. You'll need to format your code better as well, that was really hard to read...
Now, notes on your code, don't try and do everything in one method, it's ugly and hard to read and debug. Methods should do one thing (not multiple things like getting user input, translating, output) and do it well. The reason behind this is so that methods purpose can be understood better and faster if you can read the whole thing on one screen.
Also, variables should start with a lower case letter, so this can be confusing:-