I tried creating a little programm, that randomly puts chars together (in this case 2 to keep it simple) until it gets the requested combination (here: "ab"). But unfortunately I seem to have trouble adding the char "rand" onto the string "string", because the comparison in the while-loop doesn't seem to work even though it has the right combination. Could it be, that Java adds invisible characters when you create a String? How can I add the char properly?
public static void main(String[] args) {
// TODO Auto-generated method stub
String string = "";
String comp = "ab";
int counter = 0;
char rand;
while (string != comp) {
string = "";
for (int i = 1; i <= comp.length(); i++) {
rand = (char) Math.floor(Math.random() *26 + 97);
//JOptionPane.showMessageDialog(null, rand);
string += rand;
//JOptionPane.showMessageDialog(null, string);
}
//JOptionPane.showMessageDialog(null, string + ", " + counter);
counter++;
System.out.println(string + ", " + counter);
}
JOptionPane.showMessageDialog(null, string);
JOptionPane.showMessageDialog(null, "Achieved after " + counter + " tries");
}