0

Need help with simple code

this is the code: class MyClass { public static void main(String[ ] args) { String name ="David"; int age = 42; double score =15.9; char group = 'Z'; if String != Hank; System.out.println("Hello :)"); } } how do I make it so if string != Hank then it will print "Hello :)"

8th Dec 2016, 4:27 PM
PugsAreAwsome
PugsAreAwsome - avatar
2 Answers
+ 4
you forgot to put Hank inside a pair of quotation marks. Also you should not put dataType in the other side of comparison. Use the variable's name. like this if name != "Hank" { System out.println("hello"); }
8th Dec 2016, 4:41 PM
Alireza M
Alireza M - avatar
- 1
use .equals() when compare with strings. example: a.equals(b) or !a.equals(b) for your qn, u can write this: if(!name.equals("Hank")){ System.out.println("Hello :)"); }
9th Dec 2016, 1:59 AM
lowshuen