The premise is a brute force search for an inputted string. Based on the length of the inputted string (1-4) the switch statement executes one of four blocks which should break when it finds the string.
The problem is that it works for a single char just fine, but anything more than that it will execute multiple blocks and ignore my break statement.
The code:
import java.io.IOException; import java.util.*; public class Guesser { public static void main(String[] args) throws IOException { char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 .,?!:;'-".toCharArray(); Scanner sc = new Scanner(System.in); System.out.println("Input 1-4 characters"); String characters = sc.next(); int len = characters.length(); switch (len) { case 4: for (int i=0; ; i++) { // FOUR LETTER STRING int w = (int) (Math.random() * alphabet.length); int x = (int) (Math.random() * alphabet.length); int y = (int) (Math.random() * alphabet.length); int z = (int) (Math.random() * alphabet.length); char letterOne = (alphabet[x]); String firstPlace = String.valueOf(letterOne); char letterTwo =(alphabet[y]); String secondPlace = String.valueOf(letterTwo); char letterThree =(alphabet[z]); String thirdPlace = String.valueOf(letterThree); char letterFour =(alphabet[w]); String fourthPlace = String.valueOf(letterFour); System.out.println(firstPlace + secondPlace + thirdPlace + fourthPlace); if (characters.equals(firstPlace + secondPlace + thirdPlace + fourthPlace)) { System.out.println("That took " + i + " tries." ); break; } } case 3: for (int i=0; ; i++) { // THREE LETTER STRING int x = (int) (Math.random() * alphabet.length); int y = (int) (Math.random() * alphabet.length); int z = (int) (Math.random() * alphabet.length); char letterOne = (alphabet[x]); String firstPlace = String.valueOf(letterOne); 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.equals(firstPlace + secondPlace + thirdPlace)) { System.out.println("That took " + i + " tries." ); break; } } case 2: for (int i=0; ; i++) { // TWO LETTER STRING int x = (int) (Math.random() * alphabet.length); int y = (int) (Math.random() * alphabet.length); char letterOne = (alphabet[x]); String firstPlace = String.valueOf(letterOne); char letterTwo =(alphabet[y]); String secondPlace = String.valueOf(letterTwo); System.out.println(firstPlace + secondPlace); if (characters.equals(firstPlace + secondPlace)) { System.out.println("That took " + i + " tries." ); break; } } case 1: for (int i=0; ; i++) { // ONE LETTER STRING int x = (int) (Math.random() * alphabet.length); char letterOne = (alphabet[x]); String firstPlace = String.valueOf(letterOne); System.out.println(firstPlace); if (characters.equals(firstPlace)) { System.out.println("That took " + i + " tries." ); break; } } // default: // System.out.println("That's not 1-4 characters."); // break; } } }
It'd be a lot easier if I could break this down into separate methods and keep track of them, but when broken down the scope of the variables is out of range...
Sample output (for inputted string '1'):
1
J
c
[...]
O
1
That took 51 tries.
Sample output (for inputted string 'ba'):
Input 1-4 characters
ab
C.
rv
?I
;H
0L
Uf
Ug
4r
[...]
K // here it starts outputting and searching single characters? Infinite loop.
H
8
J
6