0

I'm trying to find the employee's salary by entering their rating of excellent, good, or poor through the scanner input. I'm trying to use the if statements to do so but the output is the else statement Wrong input.

import java.util.Scanner;

public class Salary {
    public static void main(String[] args) {
        
        double Currentsalary;
        double raise;
        double newraise = 0;
        String rating;
        
        Scanner scan = new Scanner(System.in);
        
        System.out.print("Enter employees current Salary ");
        Currentsalary = scan.nextDouble();
        
        System.out.print("Enter employees rating (good, excellent or poor):");
        rating = scan.next();
        
        if (rating == "excellent") {
            raise = 6/100 * Currentsalary;
            newraise = raise + Currentsalary;
        }
            
        if (rating == "good") {
            raise = 4/100 * Currentsalary;
            newraise = raise + Currentsalary;
            
        }
        
        if(rating == "poor") {
            raise =1.5/100 * Currentsalary;
            newraise = raise + Currentsalary;
            
        }
        
        else {
            System.out.println("Wrong input");
        }
        
        System.out.println("Employees new salary is :"+ newraise);
    }
}
GURU Shreyansh
  • 881
  • 1
  • 7
  • 19
johanisani
  • 35
  • 5
  • 10
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – maloomeister Jul 12 '21 at 14:02
  • 2
    I think you will also have problems with `6/100` and `4/100`, as those are *integer* expressions that will result in zero. Use `6.0/100` and `4.0/100` instead. – Fred Larson Jul 12 '21 at 14:05
  • 1
    @johanisani I see the topic has been closed. But my suggestion - usually (in real application) you would use java build in class named BigDecimal (or BigInteger if you don't care about decimals) to make any computation on numbers. I suggest to use them. Cheers! – Pawel Kiszka Jul 12 '21 at 14:19
  • 1
    Another suggestion — follow the Java Naming Conventions: variable names and method names are written in camelCase, and class names in PascalCase. That means that `Currentsalary` should be `currentsalary` (or even better: `currentSalary`). – MC Emperor Jul 12 '21 at 15:17
  • 1
    You may also want the second and third `if` statement to be replaced with `else if`s. In the current form (after the `==` problem has been fixed), the program outputs "Wrong input" if `rating` is not equal to "poor", even if the rating is "excellent" or "good". – MC Emperor Jul 12 '21 at 15:20

0 Answers0