append characters in arraylist in a specific position(将arraylist中的字符附加到特定位置)
问题描述
public void reveal(char c) {
    for(int i = 0; i < sizeReset; i++) {
        if(wordCompare.charAt(i) == c)  revealed.set(i);
        if(revealed.cardinality() == 0) {
            eo.add(String.valueOf('*'));
        }
        else {
            if(revealed.get(i) == true) {
                eo.add(String.valueOf(wordCompare.charAt(i)));
            }
            else {
                eo.add(String.valueOf('*'));
            }
        }
}
    System.out.println(eo.toString());
    jTextPane1.setText(eo.toString().replace("[", "").replace("]", "").replace(",", "")); 
}
Note:
String wordCompare - contains the word to guess.
char c - is the letter being entered by the user
revealed - is a declared as a BitSet
int sizeReset - is the size of the word to guess
ASSUME THAT ArrayList eo = new ArrayList(); is declared outside the method. (global)
=============================================
This is what I have so far. Let's say I'll be guessing "banana". the entered letter by the user will be shown, and the rest will be marked as "*".
===========================================
PROBLEM: It appends the entire values including the "" to the end of the stored values in the arrayList and not in the specific position that I want them to be.(also the "" should not be included when it appends. only the letters that the user entered). *THE LOGIC OF THE HANGMAN GAME.
SAMPLE:
first letter entered: "a"
OUTPUT:
_a_a_a
second letter entered: "n"
OUTPUT:
_ _n_n_
(which is wrong)
The correct OUTPUT should be:
_anana
===============================================
I'm new to String manipulation and I'm having trouble with this part. I've read something about StringBuilder but I haven't figured it out yet. It would be really great if you can help me. Thanks in advance. :)
You're right, this can be done using StringBuilder. You just need to replace the chars at the indices in the original String that contained the char with the char, which can be done using setCharAt.
The following code also keeps whitespace characters the same, which is the reason for the special treatment of whitespaces in the constructor.
import java.text.MessageFormat;
public class StringReveal {
    private final String original;
    private final StringBuilder stringBuilder;
    private String currentString;
    private int missingLetters;
    public StringReveal(String original, char placeholder) {
        if (original.indexOf(placeholder) >= 0) {
            throw new IllegalArgumentException("placeholder must not be contained in the string");
        }
        if (Character.isWhitespace(placeholder)) {
            throw new IllegalArgumentException("placeholder must not be a whitespace character");
        }
        this.original = original;
        this.stringBuilder = new StringBuilder();
        this.missingLetters = original.length();
        for (char c : original.toCharArray()) {
            if (Character.isWhitespace(c)) {
                // keep whitespace characters the same
                missingLetters--;
                stringBuilder.append(c);
            } else {
                // replace everything else with placeholder char
                stringBuilder.append(placeholder);
            }
        }
        this.currentString = stringBuilder.toString();
    }
    public void reveal(char c) {
        int index = original.indexOf(c);
        if (index < 0 || currentString.charAt(index) == c) {
            // if letter was already replaced or does not exist in the original
            // there's nothing to do
            return;
        }
        do {
            // replace char
            stringBuilder.setCharAt(index, c);
            missingLetters--;
            // find next char
            index = original.indexOf(c, index + 1);
        } while (index >= 0);
        currentString = stringBuilder.toString();
    }
    public String getCurrentString() {
        return currentString;
    }
    public int getMissingLetters() {
        return missingLetters;
    }
    public static void main(String[] args) {
        StringReveal stringReveal = new StringReveal("banana bread", '*');
        char[] guesses = {'a', 'x', 't', 'a', 'b', 'k', 'n', 'g', 'd', ' ',  '*', 'r', 'w', 'e'};
        System.out.println("Try to guess this sting: " + stringReveal.getCurrentString());
        for (char guess : guesses) {
            stringReveal.reveal(guess);
            System.out.println(MessageFormat.format("Current String: {0} ; guess: ''{1}''; you still have to guess {2} letters",
                    stringReveal.getCurrentString(),
                    guess,
                    stringReveal.getMissingLetters()));
        }
    }
}
这篇关于将arraylist中的字符附加到特定位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将arraylist中的字符附加到特定位置
				
        
 
            
        - 获取数字的最后一位 2022-01-01
 - 转换 ldap 日期 2022-01-01
 - 未找到/usr/local/lib 中的库 2022-01-01
 - java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
 - 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
 - Eclipse 的最佳 XML 编辑器 2022-01-01
 - GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
 - 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
 - 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
 - 如何指定 CORS 的响应标头? 2022-01-01
 
						
						
						
						
						
				
				
				
				