1

I am taking a programming class and set up code about computing a paycheck. Everything works fine except for line 11. I end up getting a stackoverflow error.

However when I remove this line of code

double weeksWages = pay(50, 10); // weeksWages is 550

The error goes away, but when running program, I end up with 10 instead of 550 that is intended. This is probably really simple to fix, but not sure. Thanks!

Here is the full code:

import java.util.Scanner;
import static java.lang.System.out;

public class ComputePayCheck {    

  static Scanner in = new Scanner(System.in);


  public static double pay(int hours, double hourlyRate) {
     int otHours = (hours > 40) ? hours - 40 : 0;
     double weeksWages = pay(50, 10); // weeksWages is 550
     return otHours;
  }

  public static void main(String[] args) {
     out.print("Enter hours worked: ");
     int hours = in.nextInt();
     out.print("Enter hourly rate: ");
     double hourlyRate = in.nextDouble();
     out.print("Week's Salary is: " + pay(hours, hourlyRate));


  }

}
1
  • 2
    The line double weeksWages = pay(50, 10); // weeksWages is 550 has no effect, other than generating the stack overflow. Commented Jan 31, 2016 at 1:05

1 Answer 1

2

You're getting a stack overflow because your pay function is recursively calling itself with no end in sight. I'm not sure what the exact expected behaviour of your method is, but try something like this instead.

public static double pay(int hours, double hourlyRate) {
 int otHours = (hours > 40) ? hours - 40 : 0;
 return hourlyRate * otHours;
}
Sign up to request clarification or add additional context in comments.

3 Comments

So far so good, the code is runnable. Thanks for the solution!
Glad this has worked out for you! If this has solved the problem could you go ahead and mark this answer as such? :)
It kind of solved my problem, but I'll mark it anyway. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.