+ 3

Why do i get the output "False" with the right input?

This is the Main method: public static void main(String[] args){ Dog one = new Dog(); Mouse two = new Mouse(); print("Dog "); one.givdata(); enter(); print("Mouse "); two.givdata(); enter(); System.out.println("Type py an then press Enter"); Scanner Pr1 = new Scanner(System.in); String Ppr1 = Pr1.next(); /*Here is my Problem my boolean say False boolean Pppr1 = Ppr1=="py"; if(Pppr1==true){ Progrlang py = new Progrlang(); py.pyInfo(); }else{ System.out.println(Pppr1);

10th Nov 2017, 7:09 PM
Giovanni Palluotto
Giovanni Palluotto - avatar
4 Réponses
+ 11
Use the following statement : boolean Pppr1 = Ppr1.equals("py"); Since String is object in Java, it can't be compared using == operator.
10th Nov 2017, 7:23 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 8
to compare strings use the equals() method not ==
10th Nov 2017, 7:50 PM
David Akhihiero
David Akhihiero - avatar
+ 4
Thx 😀
10th Nov 2017, 7:26 PM
Giovanni Palluotto
Giovanni Palluotto - avatar
+ 3
got it thx
10th Nov 2017, 8:01 PM
Giovanni Palluotto
Giovanni Palluotto - avatar