1

I finally got my program to compile without any errors and the first half is correct the total pay, retirement deduction and net pay were all displaying 0. I saw from a different post that Java doesn't analyze the logic of your if blocks so I edited my code to have my rate assigned to 0 and my if statement to return rate. I'm now getting the error "unexpected return value". How do I get my program to have the appropriate value depending on the user's input?

import java.util.*;
public class AcmePay {
public static void main(String[] args) throws Exception {
    Scanner keyboard = new Scanner(System.in);
    double hours;
    int shift;
    int plan;
    double rate = 0;
    double overtimePay;
    double paycheck;
    //double retirement;
   // double retirement = paycheck - (paycheck * .03);
    //double netPay = paycheck - retirement;



    System.out.println("How many hours did you work this week?");
    hours = keyboard.nextDouble();
      if ( hours <= 40 )
      {
          paycheck = hours * rate;
      }
      else
      { 
          paycheck = (40 * rate) + ((hours - 40)) * (rate*1.5);

      }
 System.out.println("What shift did you work? 1, 2, or 3?");
    shift = keyboard.nextInt();
    if (shift == 1)
    {
        rate = 17;
        return rate;
    }
    else if (shift == 2)
    {
        rate = 18.50;
        return rate;
    }
    else if (shift == 3)
    {
        rate = 22;
        return rate;
    }
Nitro
  • 87
  • 7
  • `return` is meant for the Method to return a value, not an `if` block. Remove the `return` statements to do what you want, that is assign the rate value in your `if` blocks. You already have a default value. You can also just add an `else{ rate = 0; }` to catch the final case but it's not necessary since you already gave it a default value on declaration. – Tim Hunter Mar 31 '20 at 16:57

2 Answers2

4

To print the rate, the last part of your code can be like this:

shift = keyboard.nextInt();
if (shift == 1) {
    rate = 17;
} else if (shift == 2) {
    rate = 18.50;
} else if (shift == 3) {
    rate = 22;
}
System.out.println("Rate = " + rate);

i.e. remove the return statements and then print the rate at the end. You can't return a value from a void method like main(), hence the error.

If you want to calculate the rate using a separate method, you would do something like this:

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}

This method returns a double, so now you can (and have to) use return statements.

You would call it from the main method with:

double rate = rateForShift(shift);

It's a good idea to split your code into focused methods, like this, because it makes it easier to read and work with.


I think your code has a "logic" bug in it because you are using the rate variable to calclulate paycheck, but the rate variable is always 0 at the point you use it. You should probably ask both questions before you calculate the paycheck amount.

A full program would look like this:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many hours did you work this week?");
    double hours = keyboard.nextDouble();

    System.out.println("What shift did you work? 1, 2, or 3?");
    int shift = keyboard.nextInt();

    double paycheck = calculatePayCheck(rateForShift(shift), hours);
    System.out.println("Paycheck = $" + paycheck);
}

private static double calculatePayCheck(double rate, double hours) {
    if (hours <= 40) {
        return hours * rate;
    } else {
        return (40 * rate) + ((hours - 40) * (rate * 1.5));
    }
}

private static double rateForShift(int shift) {
    if (shift == 1) {
        return 17;
    } else if (shift == 2) {
        return 18.50;
    } else if (shift == 3) {
        return 22;
    }
    return 0;
}

In Java programs you don't need to declare your variables at the top. The convention is to declare them as you need them.

Example

How many hours did you work this week?
20
What shift did you work? 1, 2, or 3?
2
Paycheck = $370.0


It's also worth mentioning that, although fine for a toy example, in a real system you should not use floating point numbers (like double) to represent money.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Wow thank you what a great answer. I know I really need to get into the habit of using methods more it's just hard for me to understand their connections and how to implement them. Unfortunately there is more to this program that I have to do. I also need to calculate a retirement plan for workers to enroll in it. Either the 2 or 3 shift have the option and they can choose when I ask them by hitting 1 for yes 2 for no. The plan is 3% of the workers gross pay is deducted from their paycheck. I started a method for it already but how would I call the paycheck in this method? – Nitro Mar 31 '20 at 17:55
  • Nitro, sorry if this sounds annoying, but I strongly suggest you try and understand how methods work (maybe play around with a simpler example) then you'll probably be able to solve your own problem. If you can't, post a new question. It's one question per question on StackOverflow! By the way, I've been programming for 35 years and I still find it challenging, but it's like a crossword puzzle. No fun if people give you the answers. – ᴇʟᴇvᴀтᴇ Mar 31 '20 at 17:58
2

ELEVATE covered the code, so I'll cover the theory.

In Java (and in many other programming languages), a method is a block of code that may or may not take in an input and may or may not give an output. Whether or not the method gives an output can be determined by analyzing the method definition. If a primitive type (for example, int or double) or Object (for example, Scanner) is used in the method definition, then that method will return that type as output. If the keyword void is used in the method definition, then that method will return no output.

A key point to understand is this: Once a method is told to return a value, that method will terminate. Thus, you cannot include any code after a return statement, or your compiler will be angry with you and yell at you for including "unreachable code."

Now, to apply this to the specifics. The main method in Java uses the keyword void to indicate that it will not return any sort of output. Thus, return rate; is inappropriate for two reasons:

  1. Returning a double value is indeed some sort of output, which contradicts the method definition of main in which main was set to return no output by the keyword void.
  2. return rate; will cause your program to terminate immediately. Assuming that ELEVATE was correct about how you should reorder your code, leaving a return statement in your answer would cause you further problems by preventing your code from moving on to calculate your paycheck.

Side Note: A method that returns no output can still use the return keyword, but it cannot be used with any sort of value or object. For example, the following method is valid because return isn't paired with any sort of value.

public class ReturnExample {
        /*Other stuff.*/
        public void returnNothing() {
            System.out.println("Now returning nothing.");
            return;
        }
    }
}
  • Uh-oh. I lost the comment in the edit, but thanks to whoever it was who suggested that I change "function" to "method." (S)he posted this link for the interested: https://stackoverflow.com/questions/155609/whats-the-difference-between-a-method-and-a-function – Fluffy the Togekiss Mar 31 '20 at 19:13