+ 3
How to make if statement with user input
i want to make if statement with user input in java.
6 ответов
+ 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
+ 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
+ 3
What have you tried so far? Can you share your code attempt?
Hopefully you're continuing to work through the course?
+ 3
Duncan thank you :)
Andrew Ting Mai Zau thanks you :)
+ 2
Scanner test=new Scanner(System.in);
String ans= test.nextLine().equals("1234") ? "true" : "false";
System.out.println(ans);
//make it simple
+ 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");
}