+ 1

name="john"; name2="carlos"; if(name <> name2 ) System.out.println("different"); //why eror?

eror

23rd Feb 2017, 10:19 PM
Jatmiko Indriyanto
Jatmiko Indriyanto - avatar
4 Réponses
+ 2
error is actually in your syntax: it's because "<>" doesn't just work, rather use "!=" (for not equal to). it should and will work
23rd Feb 2017, 10:30 PM
Victor Abuka
Victor Abuka - avatar
+ 1
for strings you should use .equals() if(name.equals(name2)){ System.out.println("same"); } //use this, not an operator. plz.
24th Feb 2017, 4:09 AM
Michael Szczepanski
Michael Szczepanski - avatar
+ 1
of course mine has no output. the names are different. mine will output if they are THE SAME. you either need an else statement or a negation to see it. if(! name.equals (name2)){ diff.} if (name.equals (name2)){ same } else { diff } thats how it works. it returns a boolean. my way works just fine. you should Not be using operators at all when comparing string values in java. its a Terrible idea. and I've only ever seen <> as a negation in sql.
24th Feb 2017, 4:42 PM
Michael Szczepanski
Michael Szczepanski - avatar
0
Michael I'm afraid your method doesn't just work. produces no output
24th Feb 2017, 9:10 AM
Victor Abuka
Victor Abuka - avatar