开发者

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 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) 开发者_Python百科?
// 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 :)


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.


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.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜