I'm creating a game of gallows for further learning of programming logic.
1- I will mask textfield
with ***
so the user can not see the word.
In my example, I created 3 buttons , respectively "A", "B" e "C"
.
Let's suppose that the word to be guessed is "mara"
, when I click the "A"
button, I can find the position of the letter a in the String . They are in posição 2 e 4
. Since the text is masked with "*"
, when I click the "A"
button, I would like jtextfield
to display all "A"
.
It would look like this:
Example: *a*a
But I can not figure out how to do it just those letters.
Here is the code for my simple verifiable example:
String p;
int tamanho;
Scanner input = new Scanner(System.in);
public Principal() {
initComponents();
jba.addActionListener(this);
jbb.addActionListener(this);
jbc.addActionListener(this);
System.out.println("Digite uma palavra: ");
p = input.nextLine();
String replaceP = p.replaceAll("[a-zA-Z]", "*");
jTextFieldPalavra.setText(replaceP);
}
@Override
public void actionPerformed(ActionEvent ae) {
int tamanho = p.length();
if(ae.getSource() == jba) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("a")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
} else if(ae.getSource() == jbb) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("b")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
} else if(ae.getSource() == jbc) {
for(int i=0; i<tamanho; i++) {
if(p.substring(i, i+1).equals("c")) {
int posicao = i+1;
System.out.println("Está na posição " + posicao);
//System.out.println(p.substring(0, posicao));
}
}
}
}