+ 1
Please, help me. What's wrong? Whatever I do, always print "Error".
import java.util.Scanner; public class JavaApplication5 { public static void main(String[] args) { Scanner scn = new Scanner (System.in); System.out.println("Say Hello"); String s = scn.nextLine(); if(s=="Hello"){ System.out.println("Hello"); } else{ System.out.println("Error"); }
5 odpowiedzi
+ 3
I got a solution instead of if (s == "Hello") do if (s.equals("Hello"))
== works on primitives (ints, floats, chars ect)
.equals() works on objects which Strings are
+ 2
You try to check a specific string and whether the word is equivalent to the word "Hello" and if that is what you are trying to do then
https://code.sololearn.com/c7nQuRpg0K66/?ref=app
+ 1
= is for assigning values to a variable. Ex) int x = 5;
== is for testing equivalence of primitives, like ints, floats, longs. Ex) 5 == 5;
.equals(Object o) is for testing equivalence of objects, which is what you need to use for testing Strings
0
looks fine other than the brackets aren't closed also a link is much better than raw code unless it's a few lines
0
TurtleShel, thank you very much. why "=" doesn't work?