import java.io.IOException; import java.util.*; public class Guesser { public static void main(String[] args) throws IOException { char[] alphabet = "abcdefghijklmnopqrstuvwxyz1234567890 .,:;'-".toCharArray(); Scanner sc = new Scanner(System.in); System.out.println("Input three characters"); // gather three letter word to be matched with randomly generated chars String characters = sc.next(); for (int i=0; ; i++) { int x = (int) (Math.random() * alphabet.length); // randomly generate a char from the char array int y = (int) (Math.random() * alphabet.length); int z = (int) (Math.random() * alphabet.length); char letterOne = (alphabet[x]); String firstPlace = String.valueOf(letterOne); // convert generated char to string for concatenation char letterTwo =(alphabet[y]); String secondPlace = String.valueOf(letterTwo); char letterThree =(alphabet[z]); String thirdPlace = String.valueOf(letterThree); System.out.println(firstPlace + secondPlace + thirdPlace); if (characters == (firstPlace + secondPlace + thirdPlace )) { System.out.println("That took " + i + " tries." ); break; // stop the program when it finds the inputted string } } } }
I'm writing a program which will take a three letter word (for now) and then try to guess the word over and over again until it finds it, then print the word and the amount of tries it took to find it.
The problem: at the moment the program will find the word but not break out of the for loop when it does. I think it doesn't like the char to String conversion somewhere along the line. There must be a quick fix that I'm missing.