hi
I am getting this an java.lang.array index out of bound exception: 2 error - I am wondering if this is where am getting it. look it over many times, and I know you all are tired of me but am learning.

I like the array but it can be annoying at time.

here is what I am talking about.

 int[] arrgsalary = new int [2];
        int lowest = arrgsalary[0];
        int highest = arrgsalary[0];

        System.out.println("please enter the gross salary for 2 employees");

        for (i = 0; i<arrgsalary.length; i++) - I am wondering if this is the error here.
        {
            System.out.println((i+1) + ":");

            arrgsalary[i]= userinput.nextInt();

            if (arrgsalary[i] > highest)
            {
                highest = arrgsalary[i];
            }
            if(arrgsalary[i] < lowest)
            {
              lowest = arrgsalary[i];

              System.out.println("the highest gross salary is:$ " + gsalary[i]);
              System.out.println("the lowest gross salary is:$ " + gsalary[i]);
            }
        }


        this is the output
        how many employees do you wish to processed
2
please enter the regular employee number
MA213
please enter the regular employees first name 
MaryAnn
please enter the regular employees last name
Adamson
what are the regular employee of hours worked
160
what is your qualification code
R
what is the regular worker fixed salary
7800
payslip
the employee number is MA213
the employee name is MaryAnnAdamson
the employee typecode  is R
the employee deduction is: 33.0
the employee gross salary is: $ 7800.0
the employee net salary is: $ 33.0
what is the lecturer id number
MW222
what is the lecuturer first name
marva
what is the lecturer last name
wilkinson
what are the lecturer hours
160
what is the lecturer qualification code
L
payslip
the employee id number is MW222
the employee first name is marvawilkinson
the employee typecode is L
the employee deduction is 0.0
the employee gross salary is 0.0
the employee net salary is 0.0
please enter the gross salary for 2 employees
1:
8890
2:
9908

employee pay record
the number of regular workers process are 0.0
the number of lecturers processed are 0.0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at luana_abcpayroll.Luana_ABCPayRoll.main(Luana_ABCPayRoll.java:207)
Java Result: 1

Recommended Answers

All 5 Replies

hey

I found the problem. the java lang.arrray index out of bound exception: i delete it and it working but my calculation is out of whack.
am not getting the result that i am looking for. explain to me what and where my calculation is wrong and out of whack

public static double calRegwrkrs(double hrswrk, double fsalary) {

        double  rateofpay = 0, gsalary = 0, doubletme;

        if (hrswrk < 160) 
        {
            rateofpay = (fsalary/160);
            gsalary = (hrswrk * rateofpay);
        } 
        else if (hrswrk > 160) 
        {
            doubletme =(hrswrk - 160) * 2 *(fsalary/160);
            gsalary = fsalary + doubletme;
        } 
        else if (hrswrk == 160) 
        {
            gsalary = fsalary;
        }
        return gsalary;
    }
    public static double calGrossLecturer(double hrswrk, int usercode) {

        double gsalary = 0;
        double rateofpay = 0;


        if (usercode == 1) 
        {
           gsalary = (575 * hrswrk) + 2500;
           rateofpay = (hrswrk * rateofpay) + 2500;
        } 
        else if (usercode == 2) 
        {
          gsalary = (325 * hrswrk);
          rateofpay = (hrswrk * rateofpay) + 1250;
        }
        return gsalary;
}
    public static double calDeduction( double gsalary){

        double incometax = 0.25, hsurchge1 = 33.00, hsurchge2 = 19.20;
        double total_deduct = 0, totalgsalary;

        if(gsalary > 500) 
        {
            total_deduct = 33.00;
        }
        else if(gsalary > 500 && gsalary < 5000)
        {
           total_deduct = 19.20; 
        }
        else if(gsalary > 5000)
        {
            total_deduct = (5000- (gsalary * 0.25) + 19.20 + 33.00);

        }
        return total_deduct;
    }

    public static double calNsalary(double total_deduct, double gsalary){

        double  nsalary, totaldeduct = 0;

        nsalary = (gsalary - totaldeduct);

        return nsalary;
    }

    public static double totalgsalary(double gsalary){

        double totalgsalary = 0;

        totalgsalary += gsalary;

        return totalgsalary;
    }

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


        String codeStr = null;
        char code = 0;
        int wrkrs = 0;
        String usercode2 = null;
        int usercode = 0;

        String empnum = null, reg_wrkrs, lecturer = null;
        int i;
        double hrswrk,  rate_of_pay,  fsalary = 0, countreg = 0, count_lecturer = 0;
        double highestgsalary = 0, lowestgsalary = 0;
        String regwrkrs = null;

        String firstnme[] = new String[2];
        String lastnme[] = new String[2];
        char empcode[] = new char[code];
        double total_deduct[] = new double[2];
        double gsalary[] = new double[2];
        double nsalary[] = new double[2];


        System.out.println("how many employees do you wish to processed");
        int numofwrkrs = userinput.nextInt();

        for (i = 0; i < 1; i++) {

        if(code == 'r' || code == 'R')
        {
            codeStr = regwrkrs;
        }

        System.out.println("please enter the regular employee number");
        empnum = userinput.next();

        System.out.println("please enter the regular employees first name ");
        firstnme[i] = userinput.next();

        System.out.println("please enter the regular employees last name");
        lastnme[i] = userinput.next();

        System.out.println("what are the regular employee of hours worked");
        hrswrk = userinput.nextDouble();

        System.out.println("what is your qualification code");
        usercode2 = userinput.next();

        System.out.println("what is the regular worker fixed salary");
        fsalary= userinput.nextDouble();

        gsalary[i] = calRegwrkrs(hrswrk, fsalary);
        total_deduct[i] = calDeduction(gsalary[i]);
        nsalary[i] = calNsalary(gsalary[i],total_deduct[i]);

        System.out.println("payslip");

        System.out.println("the employee number is " + empnum);
        System.out.println("the employee name is " + firstnme[i] + lastnme[i]);
        System.out.println("the employee typecode  is " + usercode2);
        System.out.println("the employee deduction is: " + total_deduct[i]);
        System.out.println("the employee gross salary is: $ " + gsalary[i]);
        System.out.println("the employee net salary is: $ " + nsalary[i]);

        }
        for(i=0; i < 1; i++){


        if(code == 'L' || code == 'l')
        {
           codeStr = lecturer;
        }

        System.out.println("what is the lecturer id number");
        empnum = userinput.next();

        System.out.println("what is the lecuturer first name");
        firstnme[i] = userinput.next();

        System.out.println("what is the lecturer last name");
        lastnme[i] = userinput.next();

        System.out.println("what are the lecturer hours");
        hrswrk = userinput.nextDouble();

        System.out.println("what is the lecturer qualification code");
        usercode2 = userinput.next();

        gsalary[i]=calGrossLecturer(hrswrk,usercode);
        total_deduct[i]= calDeduction(gsalary[i]);
        nsalary[i] = calNsalary(total_deduct[i], gsalary[i]);

        System.out.println("payslip");

        System.out.println("the employee id number is "+ empnum);
        System.out.println("the employee first name is " + firstnme[i] + "\t" + lastnme[i]);
        System.out.println("the employee usercode is " + usercode2);
        System.out.println("the employee deduction is " + total_deduct[i]);
        System.out.println("the employee gross salary is "+ gsalary[i]);
        System.out.println("the employee net salary is " + nsalary[i]);

        int[] arrgsalary = new int [2];
        int lowest = arrgsalary[0];
        int highest = arrgsalary[0];


        if(i == 0)
        {
            highestgsalary = gsalary[i];
            lowestgsalary = gsalary[i];

            if (arrgsalary[i] > highest)
            {
                highest = arrgsalary[i];
            }

            if(arrgsalary[i] < lowest)
            {
              lowest = arrgsalary[i];
            }

        }
        System.out.println("                                                 ");

        System.out.println("employee pay record");

        System.out.println("the number of regular employees process are " + countreg);
        System.out.println("the number of lecturers processed are " + count_lecturer);
        System.out.println("the total gross salary for all employees are " + gsalary[i]);
        System.out.println("the highest gross salary is:$ " + highestgsalary +  "\t" + "was earned by" + firstnme[i] + "\t" + lastnme[i]);
        System.out.println("the lowest gross salary is:$ " + lowestgsalary + "\t" + "was earned by" +  firstnme[i] + "\t" + lastnme[i]);

}

}
}

the output is
how many employees do you wish to processed
2
please enter the regular employee number
AM339
please enter the regular employees first name 
amanda
please enter the regular employees last name
mason
what are the regular employee of hours worked
160
what is your qualification code
R
what is the regular worker fixed salary
8960
payslip
the employee number is AM339
the employee name is amandamason
the employee typecode  is R
the employee deduction is: 33.0
the employee gross salary is: $ 8960.0
the employee net salary is: $ 33.0
what is the lecturer id number
AW225
what is the lecuturer first name
alexis
what is the lecturer last name
wilson
what are the lecturer hours
160
what is the lecturer qualification code
L
payslip
the employee id number is AW225
the employee first name is alexis   wilson
the employee usercode is L
the employee deduction is 0.0
the employee gross salary is 0.0
the employee net salary is 0.0
the highest gross salary is:$ 0.0   was earned byalexis wilson
the lowest gross salary is:$ 0.0    was earned byalexis wilson

employee pay record
the number of regular employees process are 0.0
the number of lecturers processed are 0.0
the total gross salary for all employees are 0.0
BUILD SUCCESSFUL (total time: 1 minute 24 seconds)

can somebody tell me how do I implement java random generator numbers in a game. have been trying to build this multiplication game that need to generate random numbers. how do i get to make it generate random number so that I can multiply it and for it to be reteriate until the user get it either right or wrong.

i need it to generate different random numbers from the 2 times table, 3 times table and so one. have tried out everything but it is not working

Oh dear, divinity02

139 posts and you don't yet know that you must:
1 start a new thread for a new question
and
2 show what you have done

A quick answer for your random number generator... You may start with Random class from Java...

I am wondering if this is where am getting it. look it over many times, and I know you all are tired of me but am learning.

  1. To be precise to you question. Put at try around your code. catch your exception and print the stack trace. That will definitely give you the point of origin for your exception.

  2. You claim to be learning but in a very recent post of yours, myself, @James and @Stlutuske suggested a bunch of methods for debugging out of which printStackTrace is definitely one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.