-3

I have written this code but when i am giving input Banglore and 26000 it is not returning desired condition.

I want to get all employee details when the employee address is Banglore and salary is more than 25000.

import java.util.Scanner;

public class ContactManager {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);

        int employee_id;
        String employee_name;
        String address;
        int mobile;
        int salary;
        String q;

        System.out.println("empolyee_id:");
        employee_id=s.nextInt();

        System.out.println("employee_name:");
        employee_name=s.next();

        System.out.println("mobile:");
        mobile=s.nextInt();

        System.out.println("address:");
        address=s.next();

        System.out.println("salary:");
        salary=s.nextInt();

        q = "Banglore";

        if(address==q && salary > 25000) {
            System.out.println(employee_name);
            System.out.println(employee_id);
            System.out.println(address);
            System.out.println(mobile);
            System.out.println(salary);
        } else {
            System.out.println("Sorry");
        }
    }
}
J-Alex
  • 6,881
  • 10
  • 46
  • 64
hta72
  • 1
  • 3
  • In Java, the == operator checks equality by reference. In this case, q and address won't match. You want to use the "equals" method: address.equals(q). Also consider converting the strings to upper or lowercase before comparison. – John Scattergood Jul 08 '17 at 17:09

2 Answers2

0

You must compare strings using equals() method.

== tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

Variable String address and String q = "Banglore" are not the same objects, so address == q returns false.

Replace if(address==q && salary>25000) on if(q.equals(address) && salary>25000), and it will work perfectly.

Ihor Dobrovolskyi
  • 1,241
  • 9
  • 19
0

Instead of if(address==q && salary>25000) you can use if (address.equals(q) && salary > 25000) it should work then.

Shahbaz
  • 141
  • 2
  • 7