【问题标题】:Finding more then one occurrence of a letter in a StringBuilder Object在 StringBuilder 对象中查找不止一次出现的字母
【发布时间】:2013-05-05 17:29:31
【问题描述】:

我正在尝试创建一个刽子手程序...以及其他问题,我似乎无法找到或创建一种方法来查找一个以上的字母并替换所有找到的事件。我一直在搞乱 findAndReplace() 方法试图解决这个问题......它没有奏效。请问谁能帮帮我

public class Hangman {


static Scanner sc = new Scanner(System.in);
        static final int MAXTRYS = 10;
        static int numError = 0;
        static boolean FINSHED = false;
        String s;
        String input;
        String tW;
        int pos;


        public static void main(String[] args) {
            System.out.println("Welcome to the Hangman App" + "\n");
            String s = answerKey();
            StringBuilder AnswerKey = dashReplace(s);




            while(!FINSHED && numError < MAXTRYS){
            //get user input and cast to char
            System.out.println("Enter an Letter: ");
            String input = sc.nextLine();
            char ch = input.charAt(0);


            int pos = findAndReplace(s, AnswerKey, input, ch);
            int index = AnswerKey.indexOf("-");
            if(pos == -1){
                numError++;
                continue;
            }else if(index == -1){
                FINSHED = true;
                System.out.println("you won!");
            }
            }

        }


        public static int findAndReplace(String s, StringBuilder AnswerKey,
                String input, char ch) {
            //find position of user input and replace
            int pos = s.indexOf(input);
            AnswerKey.setCharAt(pos, ch);
           // while(pos > 0) {
           //    AnswerKey.setCharAt(pos, (char) (ch+1));
            //}
            System.out.println(AnswerKey);
            return pos;
        }


        public static StringBuilder dashReplace(String s) {
            //replace non-white space char with dashes and creates StringBuilder Object
            String tW = s.replaceAll("\\S", "-"); 
            System.out.print(tW + "\n");  
            StringBuilder AnswerKey = new StringBuilder(tW);
            return AnswerKey;
        }


        public static String answerKey() {
            //get random array element
            String array[] = new String[10];
            array[0] = "Hamlet";
            array[1] = "Mysts of Avalon";
            array[2] = "The Iliad";
            array[3] = "Tales from Edger Allan Poe";
            array[4] = "The Children of Hurin";
            array[5] = "The Red Badge of Courage";
            array[6] = "Of Mice and Men";
            array[7] =  "Utopia"; 
            array[8] =  "Chariots of the Gods";
            array[9] =  "A Brief History of Time";

            ArrayList<String> list = new ArrayList<String>(Arrays.asList(array));
            Collections.shuffle(list);
            String s = list.get(0);
            //for testing
            System.out.println(s);
            return s;
        }

}

【问题讨论】:

    标签: java string indexing position stringbuilder


    【解决方案1】:

    典型的“查找和替换”循环如下所示:

    // Search starts at pos+1, so we set pos to -1 to start search at zero
    int pos = -1;
    // We'll break out of the loop when there are no further matches
    while (true) {
        // Look for the desired character starting one character past the pos
        pos = s.indexOf(input, pos+1);
        // If the character is not found, end the loop
        if (pos < 0) break;
        // Otherwise, replace the character
        s.setCharAt(pos, ch);
    }
    

    查看上面代码中的 cmets 以了解发生了什么。

    【讨论】:

    • 非常感谢您一次帮助我解决了两个问题,我非常感激!!!
    【解决方案2】:

    最简单的解决方案似乎是将您的StringBuilder 转换为String,使用replace() 方法,然后将StringBuilder 替换为新的:

    answerKey = new StringBuilder(answerKey.toString().replace(charToReplace, newChar));
    

    【讨论】:

      猜你喜欢
      • 2016-03-19
      • 2021-11-13
      • 2017-11-12
      • 2015-02-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      • 2022-10-09
      • 1970-01-01
      相关资源
      最近更新 更多