Как преобразовать строку в массив символов, а затем вернуть массив в другой класс? - PullRequest
0 голосов
/ 10 октября 2018

Мне нужно, чтобы программа принимала пользовательскую строку, а затем выводила каждый символ строки в отдельной строке.

Это метод в моем главном классе, который выполняет меню.Случай 6 - это тот, который я пытаюсь заставить работать.

  private static void executeMenuChoice(int menuChoice, StringManipulator myManipulator)
{
    switch (menuChoice)
    {
        case 1:
            String userUpperCase = myManipulator.listUppercase();
            JOptionPane.showMessageDialog(null, "Your string value converted to uppercase is: " + userUpperCase);
            break;
        case 2:
            String listVowels = myManipulator.listVowels();
            JOptionPane.showMessageDialog(null, "Here are the vowels in your string: " + listVowels);
            break;
        case 3:
            String replaceVowels = myManipulator.replaceVowels();
            JOptionPane.showMessageDialog(null, "Here is your string with values replaced by underscores: " + replaceVowels);
            break;
        case 4:
            int countVowels = myManipulator.countVowels();
            JOptionPane.showMessageDialog(null, "Here is the number of vowels in your string: " + countVowels);
            break;
        case 5:
            String reverseString = myManipulator.reverseString();
            JOptionPane.showMessageDialog(null, "Here is your string in reverse: " + reverseString);
            break;
        case 6:
            char[] character = myManipulator.newlineString();
            JOptionPane.showMessageDialog(null, "Here is your string with each character on a separate line: " + character);
            break;
        case 7:
            JOptionPane.showMessageDialog(null, "You are now exiting the program.");
            break;
    }

Это метод из моего второго класса для преобразования пользовательской строки в массив символов для вывода каждого символа строки в отдельной строке.

        public char[] newlineString() {

    String newLine = getUserString();

    char[] chars = newLine.toCharArray();

    return chars;
}

1 Ответ

0 голосов
/ 10 октября 2018

Мне кажется, проблема в том, что вы пытаетесь объединить массив с текстовой строкой.

попробуйте так:

case 6:
        char[] character = myManipulator.newlineString();
        JOptionPane.showMessageDialog(null, 
               "Here is your string with each character on a separate line: " +
                String.join("\n", character));
        break;
...