Condition statements doesn't work | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grátis
0

Condition statements doesn't work

Help master, i can't figure it out how to solve this. Here's my code : ... import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String x = input.nextLine(); String[] s = x.split(" "); int l = s.length; for (int i=0; i<l; i++) { String y = s[i]; //When i print y here, it shows y is the input in example : A if (y=="A") { s[i] = "Apple"; } else if (y=="B") { s[i] = "Banana"; } else { s[i] = "Fruit"; } } String o = String.join(" ",s); System.out.print(o); } } no matter what the input it's always be result : Fruit Fruit Fruit ..... In example : A B Output : Fruit Fruit i tried manually put y = "A" in the code to run the if-statements, it's work and give output "Apple"

10th Oct 2020, 2:22 AM
Wan
Wan - avatar
2 Respostas
+ 3
String is object so Instead of y=="A" try y.equals("A")
10th Oct 2020, 2:32 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
Thank you it's worked
10th Oct 2020, 2:45 AM
Wan
Wan - avatar