I am writing a program in java that will generate a set of random characters using numbers and letters, output them one by one, clearing the console after each character, append the character to a string, and ask the user to repeat the sequence.
My problem here is that if the program says, 'a' and asks for input, even if 'a' is entered, it returns incorrect. Here is the code of the generating and testing of the strings:
public void generateSeq() {
try {
Random rand = new Random();
for (int i = 0; i < numChars; i++) {
Robot bot = new Robot();
c = characters.charAt(rand.nextInt(characters.length()));
System.out.print(c);
Thread.sleep(1000);
bot.keyPress(KeyEvent.VK_CONTROL);
bot.keyPress(KeyEvent.VK_L);
bot.keyRelease(KeyEvent.VK_CONTROL);
bot.keyRelease(KeyEvent.VK_L);
full = full + String.valueOf(c);
}
} catch (InterruptedException e) {
System.out.print("Error 1. Email me @ [email protected].");
} catch (AWTException e) {
System.out.print("Error 2. Email me @ [email protected].");
}
testSeq();
}
And here is the testing method:
public void testSeq() {
Scanner sc = new Scanner(System.in);
System.out.print("Your attempt: ");
user = sc.nextLine();
if (user == null ? full == null : user.equals(full)) {
System.out.println("Correct! Trying next combo....");
numChars++;
generateSeq();
} else {
System.out.println("Incorrect! Restarting game...");
start();
}
}
if (user == null ? full == null : user.equals(full)) {can be more cleanly written asif (java.util.Objects.equals(full, null)) {in Java 7+.if (user == null) is true { .. else .. } else it's false { print Incorrect }