I'll preface this question with the fact that I'm only 1 month into learning programming, and this assignment for school has me stumped. Specifically, it's the Morse Code to English translator (and vice versa)... here's the part I'm stuck on:
/*
* A program that asks the user to select which they would like to do: translate
* from English to Morse code, or Morse code to English. The list of characters
* for each language is stored using arrays. The program will then perform and return
* the translations.
*/
import java.util.Scanner;
public class MorseEnglishTranslator
{
public static void main( String [] args )
{
int translateChoice = 0; // Variable for person's choice for direction of translation
Scanner inputText = new Scanner( System.in ); // Create a Scanner to obtain user input
System.out.print( "Enter 1 to translate from English to Morse code, 2 for Morse code to English: " );
translateChoice = inputText.nextInt();
if (translateChoice == 1);
{
System.out.print("Enter a letter, word, or phrase you would like translated: ");
}
if (translateChoice == 2);
{
System.out.print("Enter the Morse code you would like translated, separate letters and words with a |: ");
}
String userStr = inputText.nextLine();
translator( translateChoice, userStr);
} // Closes main
public static void translator( int translateChoice, String userStr) // Method for translating either direction
{
String english [] = {"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 s1 = String.join(" ", english); // Assigns the contents of english array to one string
String morse [] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--..", ".----", "..---", "...--", "....-", ".....", "-....", "--...", "---..",
"----.", "-----", "|"};
String s2 = String.join("|", morse); // Assigns the contents of morse array to one searchable string
if (translateChoice == 1);
{
String userStrLower = userStr.toLowerCase(); // In case user capitalized anything, changes everything to lowercase
for (int allFound=0; allFound <userStrLower.length(); allFound ++)
{
allFound = s1.indexOf( userStrLower ); // Variable 'allFound' to receive the search of s1 using userStrLower
System.out.print( allFound );
}
}
if (translateChoice == 2);
{
for (int allFound=0; allFound <userStr.length(); allFound ++)
{
allFound = s2.indexOf( userStr ); // Variable 'allFound' to receive the search of s2 using userStr
System.out.print( allFound );
}
}
} // Closes translator
} // Closes class
Strings s1 and s2 are the two options I was contemplating using, but the s1 version tells me after New there was an expected semi-colon. The join option, which I tried as String.join vs join, says no suitable constructor vs cannot find symbol respectively.