+ 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"); }

7th May 2018, 6:31 PM
A.S.
A.S. - avatar
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
7th May 2018, 6:51 PM
TurtleShell
TurtleShell - avatar
+ 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
7th May 2018, 6:56 PM
‎איתן מוצ'ה‎
‎איתן מוצ'ה‎ - avatar
+ 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
7th May 2018, 7:45 PM
Zeki Gurbuz
Zeki Gurbuz - avatar
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
7th May 2018, 6:40 PM
TurtleShell
TurtleShell - avatar
0
TurtleShel, thank you very much. why "=" doesn't work?
7th May 2018, 6:55 PM
A.S.
A.S. - avatar