1

This isn't the entire code, just the problem area. (Full code below)

if (passLength > 4) {
    System.out.println("Signup alost complete.");

    Random rand = new Random();

    int randm = rand.nextInt(100000) + 1;

    System.out.println(
            "To confirm you are not a robot, please enter this code: "
                    + randm);

    String code = userInput.next();

    if (code.equals(randm)) {
        System.out.println("Thank you, " + userName);
        System.out.println(
                "You may now login. Begin by entering your username");

        if (userInput.equals(userName)) {
            System.out.println("Now please enter you password");
        }

        // Where the rest of the program will go
    }

    else {
        System.out.println("The code entered is incorrect.");
    }

}

else {
    System.out.println("Invalid Password");
}

I am making a program where the user makes an account, then later signs in. The part I am having trouble with is a verification to ensure the user is human (they obviously are, but still). After creating their username and password, I generate and print a random int, and they have to type it back.

My problem is that the program always skips to the else statement, and prints "The code entered is incorrect." Even when I enter it perfectly.

Can anyone tell me what I'm doing wrong?

Below is the entire code, just in case.

public static void main(String[] args) {

    System.out.println("Hi! To begin please choose your username.");
    System.out.println("To do this, please enter your username below. ");
    System.out.println("This name must be at least three characters.");

    Scanner userInput = new Scanner(System.in);

    String userName = userInput.next();

    int nameLength = userName.length();

    if (nameLength > 2) {

        System.out.println("Now please enter your password.");
        System.out
                .println("This password must be at lease five characters");

        String passWord = userInput.next();

        int passLength = passWord.length();

        if (passLength > 4) {
            System.out.println("Signup alost complete.");

            Random rand = new Random();

            int randm = rand.nextInt(100000) + 1;

            System.out.println(
                    "To confirm you are not a robot, please enter this code: "
                            + randm);

            String code = userInput.next();

            if (code.equals(randm)) {
                System.out.println("Thank you, " + userName);
                System.out.println(
                        "You may now login. Begin by entering your username");

                if (userInput.equals(userName)) {
                    System.out.println("Now please enter you password");
                }

                // Where the rest of the program will go
            }

            else {
                System.out.println("The code entered is incorrect.");
            }

        }

        else {
            System.out.println("Invalid Password");
        }

    }

    else {
        System.out.println("Invalid Username");

    }

}
0

3 Answers 3

3

You are comparing a String with an integer, which can't be the same obviously.

int randm = rand.nextInt(100000) + 1;
...
String code = userInput.next();
if  (code.equals(randm)) 

To solve the problem you could convert the integer to a String and compare the strings (or the other way).

String randm = String.valueOfrand.nextInt(100000) + 1;
...
String code = userInput.next();
if  (code.equals(randm)) // Comparing string now

Edit : As @Pshemo pointed out, int code = userInput.nextInt(); will do the work given that you compare the integers with ==.

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

4 Comments

We can also use int code = userInput.nextInt(); and if (code == randm)
Indeed, this is an even simpler solution.
Thanks a lot. I have not clue how I could have missed that.
To make the code more robust, userInput.nextInt() should (always) be preceded by a call to userInput.hasNextInt() (e.g. while (!userInput.hasNextInt()) { userInput.next(); }). Otherwise, you need to prepare for handling a possible InputMismatchException that gets thrown by nextInt() if the user enters non-integer input.
1

It's because randm is int and code is String. So the code if (code.equals(randm)) always results to false.

Comments

0

You cannot compare a string and an integer. Trying taking the input as an integer instead of a String. String code = userInput.next();

Instead use:

int code= userInput.nextInt();
if(code==randm)

Or you could convert the integer to a String as well

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.