Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: if statement being ignored

  1. #1
    Junior Member
    Join Date
    Jun 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if statement being ignored

    The variable "raise" isn't classed as initialised despite trying to do so in the nested "if" statements. This is an exercise for university where all we're supposed to do is add the necessary if statements to get it to work. The if statements are ignored no matter what I do. What am I doing wrong?
    Here is the code with the if statements I entered.

    package selectionIteration;

    import java.util.Scanner;
    import java.text.NumberFormat;

    public class Raise {
    public static void main (String[] args)
    {
    double currentSalary; // employee's current salary
    double raise; // amount of the raise
    double newSalary; // new salary for the employee
    String rating; // performance rating

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter the current salary: ");
    currentSalary = scan.nextDouble();
    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
    rating = scan.next();

    // Compute the raise using if ...

    if (rating == "Excellent")
    raise = currentSalary * 0.06;
    if (rating == "Good")
    raise = currentSalary * 0.04;
    if (rating == "Poor")
    raise = currentSalary * 0.015;

    newSalary = currentSalary + raise;

    // Print the results
    NumberFormat money = NumberFormat.getCurrencyInstance();
    System.out.println();
    System.out.println("Current Salary: " + money.format(currentSalary));
    System.out.println("Amount of your raise: " + money.format(raise));
    System.out.println("Your new salary: " + money.format(newSalary));
    System.out.println();
    }
    }

  2. #2
    Junior Member
    Join Date
    Jun 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default if statement being ignored

    I am working on adding code for an exercise at university where we have to add if statements to complete the code, but the 3 nested if statements are being completely ignore, and whatever I initialise "raise" as in the code, it just adds the initialised value at the end of the code instead of the modifications attempted in the if statements. This is the code, I only added the if statements.

    package selectionIteration;

    import java.util.Scanner;
    import java.text.NumberFormat;

    public class Raise {
    public static void main (String[] args)
    {
    double currentSalary; // employee's current salary
    double raise; // amount of the raise
    double newSalary; // new salary for the employee
    String rating; // performance rating

    Scanner scan = new Scanner(System.in);

    System.out.print ("Enter the current salary: ");
    currentSalary = scan.nextDouble();
    System.out.print ("Enter the performance rating (Excellent, Good, or Poor): ");
    rating = scan.next();

    // Compute the raise using if ...
    if (rating == "Excellent")
    raise = 0.06 * currentSalary;
    if (rating == "Good")
    raise = 0.04 * currentSalary;
    if (rating == "Poor")
    raise = 0.015 * currentSalary;

    newSalary = currentSalary + raise;

    // Print the results
    NumberFormat money = NumberFormat.getCurrencyInstance();
    System.out.println();
    System.out.println("Current Salary: " + money.format(currentSalary));
    System.out.println("Amount of your raise: " + money.format(raise));
    System.out.println("Your new salary: " + money.format(newSalary));
    System.out.println();
    }
    }


    Any help will be greatly appreciated. It seems like it should have been really simple to complete. Apparently not lol.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: if statement being ignored

    The if statement is not being ignored. The problem is that the value of the expression in the if statement is always false.
    Use the equals method to compare the contents of String objects, not the == operator.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 10th, 2019, 06:17 AM
  2. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  3. Replies: 2
    Last Post: June 25th, 2013, 06:33 AM
  4. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  5. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM