How to make if statement with user input | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

How to make if statement with user input

i want to make if statement with user input in java.

22nd Aug 2018, 6:48 AM
David Agustinus
David Agustinus - avatar
6 Answers
+ 4
Scanner test = new Scanner(System.in); if (test.nextLine == "1234"){ System.out.println("true"); } else { System.out.println("false"); // i tried to put 1234 but the output always false
22nd Aug 2018, 10:03 AM
David Agustinus
David Agustinus - avatar
+ 4
Ah, nice start 😎 Have a look at this code, I'm using the .equals method. I read that in Java, the == compares by reference. The .equals method compares the value of the string. Hopefully I've explained this carefully. I'm still learning Java myself. https://code.sololearn.com/c16KZY3M1Zgl/?ref=app
22nd Aug 2018, 11:59 AM
Duncan
Duncan - avatar
+ 3
What have you tried so far? Can you share your code attempt? Hopefully you're continuing to work through the course?
22nd Aug 2018, 7:25 AM
Duncan
Duncan - avatar
+ 3
Duncan thank you :) Andrew Ting Mai Zau thanks you :)
22nd Aug 2018, 1:22 PM
David Agustinus
David Agustinus - avatar
+ 2
Scanner test=new Scanner(System.in); String ans= test.nextLine().equals("1234") ? "true" : "false"; System.out.println(ans); //make it simple
22nd Aug 2018, 1:10 PM
Andrew Ting Mai Zau
Andrew Ting Mai Zau - avatar
+ 2
you can use command line arguments for user input directly in if statements if(args[0].equals("sololearn")) { System.out.println("sololearn is best"); } else { System.out.println("ok"); }
23rd Aug 2018, 8:26 AM
Sai Chandh Pasupuleti
Sai Chandh Pasupuleti - avatar