2

So I have code that when you press the mouse it will generate some random code in a text box, then when enter is hit, it is suppose to clear said textbox and replace is with a new random string but it just picks the same random string. E.g. I click the mouse and "hps" is generated, I click enter and "hps" is generated again. Is there a way to get a new string from my generator?

        public void mouseClicked(MouseEvent arg0) {
            RandSt string = new RandSt(); 
            textField_tf.setText(string.randStr());
            }
TheAlm565
  • 31
  • 4

1 Answers1

0

chosen is not reset after generating a String. A very simple solution would just be to add this one line at the start of your method:

chosen = "";

I would advise however, to use a completely different method, for example the one explained here:

import java.security.SecureRandom;
import java.math.BigInteger;

public final class SessionIdentifierGenerator {
    private SecureRandom random = new SecureRandom();

    public String nextSessionId() {
        return new BigInteger(130, random).toString(32);
    }
}
Community
  • 1
  • 1
Gooz
  • 1,106
  • 2
  • 9
  • 20