0

I'm new at learning java, and while following a tutorial from a book, I received this error Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0. I've tried researching from the net to find out more about the error, and I could not find the answer to this. To make things worse, the book's website no longer exists when I tried to go to their website.

The program is about calculating payments for 2 types of workers, Engineers and Technicians including Over time pays which is 1.5x the original salaries for the 2 different workers. Max working hours is 160 hours, and additional hours trigger the overtime rate.

Here's the code that I wrote:

class PayCalculator3 {
    public static void main (String []args) {
    final int maxNoOverTime = 160;
    final double engineerHourlyPay = 30;
    final double technicianHourlyPay = 25.5;
    final double overTimeRate = 1.5; 
    int position = Integer.parseInt(args[0]);
    int hoursWorked = Integer.parseInt(args[1]);
    double salary;

salary = 
(position == 0) ? 
// employee is an Engineer
(hoursWorked <= maxNoOverTime) ?
    // he did not work overtime
    (hoursWorked * engineerHourlyPay)
    :
    // he worked overtime
    ((maxNoOverTime * engineerHourlyPay) + ((hoursWorked - maxNoOverTime) * (engineerHourlyPay * overTimeRate))) 
: (position == 1) ?
// if employee is a Technician
(hoursWorked <= maxNoOverTime) ?
    // he did not work Overtime
    (hoursWorked * technicianHourlyPay)
    :
    // he worked overtime
    ((maxNoOverTime * technicianHourlyPay) + ((hoursWorked - maxNoOverTime) * (technicianHourlyPay * overTimeRate)))

:
//Employee Type unknown
-1;
String str = (salary != -1) ?
        ("This month's salary >> $" + salary)
        :
        ("Who the heck are you?");
System.out.println(str);
    }
}

Thank you very much in advance for all of your kind help :)

1
  • How did you invoke the program? Include the actual command line you typed. You would get this at int position = Integer.parseInt(args[0]); if you didn't include any arguments on the command line. Commented Jun 6, 2011 at 14:38

2 Answers 2

3

You must not be passing two arguments to the program. Where you are doing args[0] it's expecting an integer passed via the command line to the program. Please add how you are invoking the program.

java PayCalculator3 10 10

You should be invoking it with two integers as the argument, as per the example above.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that was very silly of me ! I did not put in the parameters when invoked the program.
1

so basically you have to go in and call this as such in your cmd or what ever you are using else you will recieve the errors i.e. you aren't putting in parameters. enter image description here

1 Comment

Yes, I forgot to put in the parameters when I invoked the program. That was very silly of me, thank you very much for your help!

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.