0

I have the following code:

System.out.println("");
System.out.println("Enter username:");
String userName = input.nextLine();
input.nextLine();
System.out.println("Enter password:");
String userPwd = input.nextLine();
System.out.println(userName.equals("Joe"));
System.out.println(userPwd.equals("password1"));

When I type in "Joe" and "password1" at the command prompt, only the second comparison returns "true". The first one returns "false". I have not found a solution. Should I be using a different library than Scanner? Thanks.

P.S. Is there a way to also get rid of the second input.nextLine();?

posfan12
  • 2,243
  • 5
  • 31
  • 47
  • 1
    Pretty sure that's a duplicate of https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo. Post a **complete** minimal example reproducing the issue.Regarding your PS: sure. Just remove it. – JB Nizet Dec 15 '19 at 08:34
  • @JBNizet Not sure the duplicate applies, the OP is using only `nextLine()`. – Guy Dec 15 '19 at 08:39
  • In the code that the OP posted. But there is code before. The posted code doesn't even initialize the scanner, and if we add just that scanner initialization, the described issue doesn't exist. – JB Nizet Dec 15 '19 at 08:40
  • I agree it's currently off-topic, just not a duplicate of the linked question ‍♂️. – Guy Dec 15 '19 at 08:42
  • Let's say it would become a duplicate if it became on-topic. What matters to me is that the OP knows what to do, and we don't waste time on this question. – JB Nizet Dec 15 '19 at 08:44
  • It is a duplicate, thanks! – posfan12 Dec 15 '19 at 08:49

0 Answers0