I am writing a program in java that will generate a set of random characters using numbers and letters, output them one by one, clear the console after each character, add a character to the string and ask the user to repeat the sequence.
My problem is that if the program says "a" and asks for input, even if "a" is entered, it returns incorrect. Here is the code for generating and testing 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 @ xxx@gmail.com.");
} catch (AWTException e) {
System.out.print("Error 2. Email me @ xxx@gmail.com.");
}
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();
}
}
source
share