Я предопределю этот вопрос тем фактом, что я занимаюсь только программированием на один месяц, и это задание для школы меня озадачило. В частности, это Морзе английский переводчик (и наоборот) ... вот часть, я застрял на:Попытка создать строку из строкового массива
/*
* 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
строки s1 и s2 являются два варианта я созерцая с помощью, но версия s1 говорит мне, что после New была ожидаемая полуколония. Параметр соединения, который я пытался использовать как String.join vs join, говорит, что подходящий конструктор vs не может найти символ соответственно.
Пожалуйста, укажите язык, который вы используете. – sschale
О, извините, Java 8 –
Уже сделано для нее, ей просто нужно принять! – sschale