尝试遍历字符串并查找char是字母还是数字,然后将其附加到其他字符串上

问题描述 投票:0回答:1

我需要遍历字符串userInput并查找char是字母还是数字,如果是,则需要将该char附加到String endProduct。

    public static String converter(String userInput) {
        String endProduct = "";
        char c = userInput.charAt(0);
        Stack<Character> stack = new Stack<Character>();
        int len = userInput.length();
        //iterates through the word to find symbols and letters, if letter or digit it appends to endProduct, if symbol it pushes onto stack
        for (int i = c; i < len; i++) {
            if (Character.isLetter(userInput.charAt(i))) {
                endProduct = endProduct + c;
                System.out.println(c);
            }//end if
            else if(Character.isDigit(userInput.charAt(i))){
                endProduct = endProduct + c;
                System.out.println(c);
            }
java string append
1个回答
0
投票
public static String converter(String userInput) {
        String endProduct = "";
        Stack<Character> stack = new Stack<Character>();
        int len = userInput.length();
        //iterates through the word to find symbols and letters, if letter or digit it appends to endProduct, if symbol it pushes onto stack
        for (int i = 0; i < len; i++) {
            char c = userInput.charAt(i);
            if (Character.isLetter(userInput.charAt(i))) {
                endProduct = endProduct + c;
                System.out.println(c);
            }//end if
            else if (Character.isDigit(userInput.charAt(i))) {
                endProduct = endProduct + c;
                System.out.println(c);
            }
            // Push to stack if char c is not letter or digit
            else {
                stack.push(c);
            }
        }
        System.out.println(endProduct);
        return endProduct;
    }

© www.soinside.com 2019 - 2024. All rights reserved.