How do I replace a string letter when it is masked?

0

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));

        } 

    }

    }

}

At that point I already clicked the "A" button.

    
asked by anonymous 22.11.2016 / 17:28

1 answer

1

It's very simple using the method setCharAt of StringBuilder . I've created an example to illustrate (You can copy everything and run it):

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 iterate over the "secret word" ( this.secretWord ), testo if the letter of the pressed button corresponds to some character of the same and Czech if in the position of the same in the text field is an asterisk . If true, I replace the char in question with the button letter, using the StringBuilder (which in turn uses the textField String). Anyway, explaining in Portuguese makes it seem more difficult than it is. Study my example and StringBuilder, which will understand how to adapt to your rs code ..

    
23.11.2016 / 03:58