字符串方法帮助Java

时间:2014-04-09 04:12:22

标签: java string methods

public static void main(String[] args) {

    String userInput = "";      // stores the user input
    String vowels = "";     // used to store the vowels contained in the user input
    String capitalLetters = ""; // used to store the capital letters in the user input

    int menuChoice;         // stores the menu choice value entered by the user
    userInput = JOptionPane.showInputDialog("Please type some text in the box below.");

    do {
        menuChoice = Integer.parseInt(JOptionPane.showInputDialog("Please choose one option from the menue below: \n\n"
                + "1.  List all upper case letters \n"
                + "2.  List all vowels \n"
                + "3.  Replace vowels with underscore \n"
                + "4.  Show the string in reverse \n"
                + "5.  Show each letter on a separate line\n"
                + "6.  Exit \n\n"));

        switch (menuChoice) {
            case 1: // Find all the capital letters in the user input
                for (int i = 0; i < userInput.length(); i++) {
                    if (Character.isUpperCase(userInput.charAt(i))) {
                        char cap = userInput.charAt(i);
                        capitalLetters += cap + " ";
                    }
                }

                JOptionPane.showMessageDialog(null, "The capital letters in the input " + userInput
                        + " are as follows: \n\n" + capitalLetters,
                        "Capital Letters", JOptionPane.INFORMATION_MESSAGE);

                break;

            case 2: // Find all the vowels in the user input
                for (int i = 0; i < userInput.length(); i++) {
                    if ((Character.toUpperCase(userInput.charAt(i)) == 'A')
                            || (Character.toUpperCase(userInput.charAt(i)) == 'E')
                            || (Character.toUpperCase(userInput.charAt(i)) == 'I')
                            || (Character.toUpperCase(userInput.charAt(i)) == 'O')
                            || (Character.toUpperCase(userInput.charAt(i)) == 'U')) {
                        vowels += userInput.charAt(i) + " ";
                    }
                }

                JOptionPane.showMessageDialog(null, "The vowels in the input " + userInput
                        + " are as follows: \n\n" + vowels,
                        "Vowels", JOptionPane.INFORMATION_MESSAGE);

                break;

            case 3: // Replace vowels with underscores

                String underscoreVowels = userInput;

                underscoreVowels = underscoreVowels.replace('A', '_');
                underscoreVowels = underscoreVowels.replace('a', '_');
                underscoreVowels = underscoreVowels.replace('E', '_');
                underscoreVowels = underscoreVowels.replace('e', '_');
                underscoreVowels = underscoreVowels.replace('I', '_');
                underscoreVowels = underscoreVowels.replace('i', '_');
                underscoreVowels = underscoreVowels.replace('O', '_');
                underscoreVowels = underscoreVowels.replace('o', '_');
                underscoreVowels = underscoreVowels.replace('U', '_');
                underscoreVowels = underscoreVowels.replace('u', '_');

                JOptionPane.showMessageDialog(null, "The vowels in the input " + userInput
                        + " replaced by underscores looks like this: \n\n" + underscoreVowels,
                        "Vowels", JOptionPane.INFORMATION_MESSAGE);

                break;

            case 4: // Show the message entered in reverse

                String reverseString = "";

                for (int i = userInput.length() - 1; i >= 0; i--) {
                    reverseString += userInput.charAt(i);
                }
                JOptionPane.showMessageDialog(null, "The input in reverse order is: \n\n" + reverseString,
                        "Reverse Input", JOptionPane.INFORMATION_MESSAGE);

                break;

            case 5: // Show the message entered in reverse

                String multiLineString = "";

                for (int i = 0; i <= userInput.length() - 1; i++) {
                    multiLineString += userInput.charAt(i) + "\n";
                }
                JOptionPane.showMessageDialog(null, "Below is each character entered on a separate line: \n\n" + multiLineString,
                        "Reverse Input", JOptionPane.INFORMATION_MESSAGE);

                break;

            case 6: // Display a thank you message and exit the program
                JOptionPane.showMessageDialog(null, "Thanks for using my program!! \n\n Have a great day.",
                        "Exit Program", JOptionPane.INFORMATION_MESSAGE);
                break;

            default: // Display and error message if they entered a value not found on the menu
                JOptionPane.showMessageDialog(null, "*** ERROR *** \n\nThe value you entered, is not a menu option. \n\n Plesee try again.",
                        "ERROR", JOptionPane.ERROR_MESSAGE);
                break;
        }// end switch

    }// end do
    while (menuChoice != 6);
    System.exit(0);
}

好的,我只需要一些指导。但是对于上述情况中的每一个都需要有自己的方法,我需要基本上通过将每个switch-case语句中的功能移动到新方法来编辑程序。新方法应处理所有字符串操作,然后将编辑后的字符串传递回main方法以完成对用户的输出。提前谢谢!

1 个答案:

答案 0 :(得分:2)

我将通过展示案例1来帮助您朝着正确的方向前进。

正如您在评论中提到的,这种情况会覆盖某些字符串中的字符,并将所有大写字符放在由空格字符" "分隔的新字符串中。例如,Hello WorlD返回字符串H W D

因此,让我们将所有这些代码放在一个方法中。应该是直截了当的。

private static String caseOneMethod(String input) {
    //Code here
}

这非常简单。这将创建一个新方法,该方法接受String输入并生成String输出,这是您想要的。现在,您可以用以下内容替换Case 1块:

case 1: // Find all the capital letters in the user input
    capitalLetters = caseOneMethod(userInput);

    JOptionPane.showMessageDialog(null, "The capital letters in the input " + userInput + 
    " are as follows: \n\n" + capitalLetters, 
    "Capital Letters", JOptionPane.INFORMATION_MESSAGE);

    break;