我需要程序接受用户字符串,然后在单独的行上输出字符串的每个字符。
这是我的Main类中执行菜单的方法。案例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;
}
这是第二个类中的方法,用于将用户字符串转换为char数组,以在不同的行上输出字符串的每个字符。
public char[] newlineString() {
String newLine = getUserString();
char[] chars = newLine.toCharArray();
return chars;
}
答案 0 :(得分:1)
我认为问题在于您正在尝试将数组连接到文本字符串。
尝试这样:
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;