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.*/

14th Aug 2017, 5:04 AM
yogesh
yogesh - avatar
12 Respuestas
+ 2
Try to use .equals instead of "==".
14th Aug 2017, 5:07 AM
Jonas Schröter
Jonas Schröter - avatar
+ 2
First you have to install a jdk, i would recommend to use an IDE like Intellij Idea or Eclipse for development.
14th Aug 2017, 5:12 AM
Jonas Schröter
Jonas Schröter - avatar
+ 2
== compares the objects, not they're values
14th Aug 2017, 12:40 PM
Jonas Schröter
Jonas Schröter - avatar
+ 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
14th Aug 2017, 12:45 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
+ 1
use: name.equals("y")
14th Aug 2017, 5:08 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
.equals instead of ==
14th Aug 2017, 5:44 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
+ 1
you can use note pad and then use java jdk in the command lines to compile it to .class file
14th Aug 2017, 6:33 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
will this work in notepad?? or tell me other compilers?
14th Aug 2017, 5:09 AM
yogesh
yogesh - avatar
0
I used jdk with notepad 😊😊
14th Aug 2017, 6:52 AM
yogesh
yogesh - avatar
0
and hello doraeme!!😊😊
14th Aug 2017, 6:53 AM
yogesh
yogesh - avatar
0
is my code correct guys?
14th Aug 2017, 6:53 AM
yogesh
yogesh - avatar
0
and please tell why .equals working instead of == ??
14th Aug 2017, 6:56 AM
yogesh
yogesh - avatar