1

Have almost got this login system in Java to work, but am having trouble with this method:

public void Register() {
    Scanner sc = new Scanner(System.in);

    System.out.print("Register? (Y/ N)\n");
    String N = sc.nextLine();

    if ("N".equals(N)) {
        Login();
    } else {
        String Y = sc.nextLine();
        if ("Y".equals(Y)) {
        System.out.print("Email address: ");
        String string = sc.nextLine();
        System.out.print("Password: ");

        String string2 = sc.nextLine();

        System.out.print("\n\n");
        new Products().search();
        }
    }

}

Entering "N" works perfectly in the if section, but "Y" needs to be entered twice before the else section works (I understand why it doesn't work).

Its something simple I know, but any clue as to how to get it to work?

Appreciate any help...

3
  • Please please please: never name variables N or Y. Use name the mean something to human readers. Such as: inputFromUser. Just N N N. Commented Jul 9, 2018 at 11:51
  • step-by-step debugging is your best friend.... Commented Jul 9, 2018 at 11:51
  • As the best coding practices, please rename your variables to more intuitive names. Like String userInput = sc.nextLine(); String emailId = sc.nextLine(); and so on. Commented Jul 9, 2018 at 11:54

4 Answers 4

5

Here

String Y = sc.nextLine();

you are reading in another line of input. You want to compare the same line of input you already read, which you stored in a variable called N. It will be clearer if you give it a better name.

String line = sc.nextLine();

if ("N".equals(line)) {
    Login();
} else if ("Y".equals(line)) {
    System.out.print("Email address: ");
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this was what I was looking for
3

This String N = sc.nextLine(); Again This

String Y = sc.nextLine();

No need to use the input method twice

Change the variable name to something meaningful

Try it this way

It will surely work..

public void Register() {
Scanner sc = new Scanner(System.in);

System.out.print("Register? (Y/ N)\n");
String input = sc.nextLine();

if ("N".equals(input)) {
    Login();
} else {
    // removed 'String Y = sc.nextLine();'
    if ("Y".equals(input)) {
    System.out.print("Email address: ");
    String string = sc.nextLine();
    System.out.print("Password: ");

    String string2 = sc.nextLine();

    System.out.print("\n\n");
    new Products().search();
    }
}

Comments

1

You don't need the second nextLine. Keep using the String N (maybe rename it to input) and keep checking that for Y.

Comments

1

If you type Y it will be saved in N variable, it will go in the else section and here you're asking again the user, you don't need to

You have to take the input and test it, without knowing its N or Y

String choice = sc.nextLine();

if ("N".equals(choice)) {
    Login();
} else if("Y".equals(choice)){      
    System.out.print("Email address: ");
    String string = sc.nextLine();
    ...       
}else{
     System.out.println("Wrong choice");
}

Also

  • give more significant names to your variable, no string, string2 but email, pwd
  • methods name have to start in lowerCaser : Login() >> login()

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.