I'm really sorry that I can't word my question properly.
So I want to create an if-else scenario where you take string input from user and to print "Oh, so your name is xyz" if the input is correct, and to print "Oh, so your name is not xyz" if the input is wrong.
String name = "xyz";
String lol = input.nextLine();
if (lol == name) {
System.out.println("Oh, so your name is xyz");
} else {
System.out.println("Oh, so your name is not xyz");
}
input.nextLine();
input.close();
}
But after I run the file I got something like this instead:
What's your name?
xyz
Oh, so your name is not xyz
I've changed the if (lol == name)
to if (lol != name)
but it prints "Oh, so your name is xyz" even though the input is false.
What's your name?
asd
Oh, so your name is xyz
This is the whole code:
import java.util.Scanner;
public class MyClass {
static void myMethod() {
Scanner input = new Scanner(System.in);
System.out.println("Hello World");
System.out.println("What's your name?");
String name = "xyz";
String lol = input.nextLine();
if (lol == name) {
System.out.println("Oh, so your name is xyz");
} else {
System.out.println("Oh, so your name is not xyz");
}
input.nextLine();
input.close();
}
public static void main(String[] args) {
myMethod();
}
}
Thanks in advance.