How to replace a string letter when it is masked?

Asked

Viewed 165 times

0

I’m creating a hangman’s game for further learning of programming logic.

1- I masked the textfield with *** so that the user cannot see the word.

2- In my example, I created 3 buttons, being respectively "A", "B" e "C".

Suppose the word to be guessed is "mara", when I click the button "A", i can find the position of the letter a in String. They meet in the posição 2 e 4. How the text is masked with "*", when I click the button "A", I would like the jtextfield presented all the letters "A".

Would look like this:

Example: *a*a

But I can’t figure out how to show only those letters.

Follow the code of my simple checkable 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));

        } 

    }

    }

}

By now I’ve already clicked on the "A". inserir a descrição da imagem aqui

  • 1

    Can add a [mcve] of your code? I think you can apply Document to it.

  • So, this would be the example Minimum, Complete and Verifiable that I created rs. But I will try to represent better according to the link you sent me. ;)

  • The code is not executable, try to copy and save as . java and run for you to see. Anyway, look what I found on another SE site, the same game, see if you can understand the logic http://codereview.stackexchange.com/questions/82440/hangman-program-in-java

  • I understood, I didn’t pay attention to it. Thank you, I will read and try to understand.

1 answer

1


It’s very simple using the method setCharAt of Stringbuilder. I created an example to illustrate (Can copy everything and run):

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JButton;

public class MainFrame extends JFrame {

    public JPanel contentPane;
    public JTextField textField;
    public String secretWord;

    public static void main(String[] args) {
        MainFrame f = new MainFrame();
        f.setVisible(true);
    }

    public MainFrame() {
        super("Jogo da Forca");
        setSize(new Dimension(500, 300));
        this.contentPane = new JPanel();
        setContentPane(this.contentPane);
        contentPane.setLayout(new MigLayout("", "[50px,grow][50px,grow][50px,grow]", "[50px,grow][50px,grow][50px,grow][50px,grow][50px,grow][50px,grow]"));

        textField = new JTextField();
        contentPane.add(textField, "cell 1 1,grow");
        textField.setColumns(10);
        this.secretWord = "MARA";
        for (int i = 0; i < this.secretWord.length(); i++) {
            textField.setText(textField.getText() + "*");
        }

        JButton btnA = new JButton("A");
        contentPane.add(btnA, "cell 0 3,grow");
        JButton btnB = new JButton("B");
        contentPane.add(btnB, "cell 1 3,grow");
        JButton btnC = new JButton("C");
        contentPane.add(btnC, "cell 2 3,grow");
        JButton[] buttons = {btnA, btnB, btnC};
        for (JButton b : buttons) {
            b.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String letter = ((JButton) e.getSource()).getText();
                    String textFieldString = textField.getText();
                    StringBuilder replacement = new StringBuilder(textFieldString);
                    for (int i = 0; i < secretWord.length(); i++) {
                        if (letter.equals(String.valueOf(secretWord.charAt(i))) && String.valueOf(textFieldString.charAt(i)).equals("*")) {
                            replacement.setCharAt(i, letter.charAt(0));
                            textField.setText(replacement.toString());
                        }
                    }
                }
            });
        }

    }

}

As you can see, I’m going on about the "secret word" (this.secretWord), test whether the letter of the button pressed corresponds to some character of the same and check whether in the position of the same in the text field is an asterisk. If true, I replace the char in question with the letter of the button, using Stringbuilder (which in turn uses the textField string). Anyway, explaining in Portuguese makes it seem harder than it is. Study my example and Stringbuilder, which will understand how to adapt to your code rs..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.