+ 1

What is wrong with my code? Where is the mistake that I made?

import java.util.Scanner; public class Program { //Someone please tell me why doesn’t my code work. If you type alma in the scanner,it should print the eredmeny. But it outputs elbasztad all the time. public static void main(String[] args) { Scanner muvelet = new Scanner(System.in); int egyik = 734; int masik = 1000; int eredmeny = egyik + masik; String beirt = muvelet.nextLine(); if (beirt=="alma") {System.out.println(eredmeny); } else { System.out.println("elbasztad"); } } }

21st Jul 2017, 8:49 PM
Győrfy Balázs
Győrfy Balázs - avatar
2 Respuestas
+ 4
You did everything correct, except the part where you compare the String, because with strings you should use .equals() change: if(beirt=="alma") into: if(beirt.equals("alma")) That should make it do what you want.
21st Jul 2017, 9:26 PM
ChessMaster
ChessMaster - avatar
+ 1
Thank you very much, it works now :)
21st Jul 2017, 9:30 PM
Győrfy Balázs
Győrfy Balázs - avatar