4

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();
        }
}
5
  • 2
    Try not using a ternary statement in a conditional. Commented Oct 6, 2015 at 20:55
  • 1
    if (user == null ? full == null : user.equals(full)) { can be more cleanly written as if (java.util.Objects.equals(full, null)) { in Java 7+. Commented Oct 6, 2015 at 21:00
  • @takendarkk is correct. Read your ternary statement. if (user == null) is true { .. else .. } else it's false { print Incorrect } Commented Oct 6, 2015 at 21:01
  • 1
    Debug your user variable to check its value at your ternary condition ,your equal method is giving false due to unmatching Commented Oct 6, 2015 at 21:29
  • What type is' full'? Commented Oct 6, 2015 at 21:35

2 Answers 2

2

In the beginning, when full is null, you attempt to add the first character to it. But this is String Conversion, which converts a null to the String "null", and your full variable now starts with "null".

Initialize it to the empty string ("") first, at the top of generateSeq.

There is nothing wrong with your use of the ternary operator, but now the strings won't be null; they would be empty at worst. Calling equals by itself is now sufficient.

if (user.equals(full))

In addition, you may want to generate your Random object once, as an instance variable, instead of creating a new Random object every time you call generateSeq.

Sign up to request clarification or add additional context in comments.

1 Comment

Still doesnt seem to be working after using the new method of dealing with full.
0

The code appears to be fine and it works.
Do you print what contains user and full when you compare it?

if (user == null ? full == null : user.equals(full)) {
    System.out.println("Correct! Trying next combo....");
    numChars++;
    generateSeq();
} else {
    System.out.println("--user:" + user);
    System.out.println("--full:" + full);
    System.out.println("Incorrect! Restarting game...");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.