0
Please correct this. I need help
import java.util.Scanner.in; class practice { public static void main(String args []) { Scanner s1=new Scanner(System.in); String name; System.out.println("enter your name"); name=s1.next(); if(name=="y") { System.out.println("yyyyyyyyyyy"); } else { System.out.println("error"); } } } /* now listen please, when this code askes me a name i write y then which is correct according to statement but it prints the else part which is error I don't get it.*/
12 Respuestas
+ 2
Try to use .equals instead of "==".
+ 2
First you have to install a jdk, i would recommend to use an IDE like Intellij Idea or Eclipse for development.
+ 2
== compares the objects, not they're values
+ 2
== checks if the variables reference the same object. .equals checks if the objects are similar, such as if the data they contain are identical. to be more technical, == checks if the memory address of two variables are the same. if they are, they must be referencing the same object which is why == doesn't work. the addresses of the two strings are different
+ 1
use:
name.equals("y")
+ 1
.equals instead of ==
+ 1
you can use note pad and then use java jdk in the command lines to compile it to .class file
0
will this work in notepad??
or tell me other compilers?
0
I used jdk with notepad 😊😊
0
and hello doraeme!!😊😊
0
is my code correct guys?
0
and please tell why .equals working instead of == ??