#Java next line

8 messages · Page 1 of 1 (latest)

sly pulsar
#

public class Test {
    public static void main(String[] args) {
        Scanner kdb = new Scanner(System.in);
        String userInput = kdb.nextLine();
        System.out.print(userInput=="d");// prints false even if I enter "d"
    }
}```
shadow talonBOT
#

This post has been reserved for your question.

Hey @sly pulsar! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

sly pulsar
marsh talon
#

use userInput.equals("d") instead of userInput == "d"

#

== compares instances, .equals() compares the values

#

For example:

String s1 = "Hey bro";
String s2 = "Hey bro";
String s3 = new String("Hey Bro");

System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
Output:

true
false
true
shadow talonBOT
#

Looks like you're having some trouble comparing strings, check out this stackoverflow question for help.