+ 2

Why does this code doesn't work?

I'm making some basic commands just to learn. The thing is that I'd made this "if" command but it is true only when B is 10. However, if I spell "caca" it doesn't activates. I would like to know if there is anything I am doing wrong and u can help me. The only thing that's missing here is the print code but I haven't wrote it as it's not necessary for the question String A = sc.nextLine (); int B = sc.nextInt (); if ( A == "caca" || B >= 10){

5th Jun 2019, 10:41 PM
DJew
DJew - avatar
5 Answers
+ 2
The problem is A == "caca". For Strings you need the equals() method: If(A.equals("caca") || B >= 10) Here is an article about the difference between == and equals(): https://www.google.de/amp/s/www.geeksforgeeks.org/difference-equals-method-java/amp/
5th Jun 2019, 10:56 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
A.equals("caca") -> return false !(false) = true A.equals("caca") -> return true !(true) = falsbe if(!(A.equals("caca"))){ System.out.println("not equal"); }
5th Jun 2019, 11:27 PM
Denise Roßberg
Denise Roßberg - avatar
+ 2
Another example: 4 == 3 -> would return false 4 != 3 -> would return true !(4 == 3) -> would return true
5th Jun 2019, 11:31 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
Denise Roßberg Thx, i've already corrected it. And if I want to make a difference ( != )for Strings, I have to write !("caca") or there is another word for it?
5th Jun 2019, 11:17 PM
DJew
DJew - avatar
+ 1
Sometimes you are missing some small but important parts, so I would recommend looking through your code to check for any mistakes before you press the run button.
5th Jun 2019, 11:52 PM
PeculiarP
PeculiarP - avatar