Nick D.
Nick D.

Reputation: 59

Java String comparison ALWAYS returns false

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();
        }
}

Upvotes: 4

Views: 1106

Answers (2)

Sergio
Sergio

Reputation: 1

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...");
}

Upvotes: 0

rgettman
rgettman

Reputation: 178343

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.

Upvotes: 2

Related Questions