Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: java.lang.ArrayIndexOutOfBoundsException help

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default java.lang.ArrayIndexOutOfBoundsException help

    Hi im fairly new to Java and im trying out this program that would be used for managing staff members.

    However I have problems with options after you enter number while no errors come up it doesn't do what its meant to. With addStaff this is an exception, i get the 'java.lang.ArrayIndexOutOfBoundsException: 10' for lines 231, 121 and 40, can anyone please help me understand what I have done wrong here?

    import java.util.*;
     
    public class StaffManager
    {
        private static int numStaff;
        private static int[] payNum;
        private static String[] surname;
        private static String[] firstName;
        private static String[] dOB;
        private static String[] dept;
        private static Scanner keyboard = new Scanner(System.in);
        private static int i;
     
        public static void main (String[] args)
        {
    	numStaff = 0;
    	final int MAX_STAFF = 10;
    	payNum = new int[MAX_STAFF];
    	surname = new String[MAX_STAFF];
    	firstName = new String[MAX_STAFF];
    	dOB = new String[MAX_STAFF];
    	dept = new String[MAX_STAFF];
     
    	initialiseArrays();
     
    	String entry;
    	char option;
     
    	do
    	{
    	    displayMenu();
    	    System.out.print("\n\t\tEnter choice (1/2/3/4/5/6/7): ");
    	    entry = keyboard.next();
    	    entry = entry.toLowerCase();
    	    option = entry.charAt(0);
    	    switch (option)
    	    {
    	    	case '1': displayAllStaff();
    	    		break;
    	    	case '2': addStaff(MAX_STAFF);
    	    		break;
    	    	case '3': removeStaffMember();
    	    		break;
    	    	case '4': displayStaffMember();
    	    		break;
    	    	case '5': displayDeptStaff();
    	    		break;
    	    	case '6': changeStaffDept();
    	    		break;
    	    	case '7': //Do nothing. ('Quit' option)
    	    	    	break;
    	    	default: System.out.println("\t\tInvalid entry! Try again...");
    	    }
    	    if(option != 7)
    	    {
    		System.out.print("\n\t\tPress the enter key to continue");
    		keyboard.nextLine();
    		keyboard.nextLine();
    	    }
    	}while (option != '7');
        }
     
        public static void initialiseArrays()
        {
    	int[] tempPayNum = {123456, 234567, 345678, 456789, 567890, 678901, 789012}; 
    	String[] tempSurname = {"Brown", "Jenkins", "Turner", "Cavendish", "Williams", "Ford", "Carter"};
    	String[] tempFirstName =  {"James", "Susan", "Jane", "Paul", "James", "Karen", "Daniel"};
    	String[] tempDOB =  {"24/3/85", "11/12/80", "5/7/72", "22/9/76", "19/1/81", "15/8/79", "4/2/62"};
    	String[] tempDept =  {"Marketing", "Sales", "Sales", "Accounts", "Marketing", "Sales", "Accounts"};
     
    	for (int i=0; i<6; i++)
    	{
    	    payNum[i] = tempPayNum[i];
    	    surname[i] = tempSurname[i];
    	    firstName[i] = tempSurname[i];
    	    dOB[i] = tempDOB[i];
    	    dept[i] = tempDept[i];
    	    numStaff++;
    	}
        }
     
        public static void displayMenu()
        {
    	System.out.println("\n\n\n");
    	System.out.println("\t\t1: Display all Staff.");
    	System.out.println("\t\t2: Add Staff.");
    	System.out.println("\t\t3: Remove Staff Member.");
    	System.out.println("\t\t4: Display Staff Member.");
    	System.out.println("\t\t5: Display Department Staff.");
    	System.out.println("\t\t6: Change Staff Department.");
    	System.out.println("\t\t7: Quit.");
        }
     
        public static void displayAllStaff()
        {
    	System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
    	System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
    	for (int i=0; i<numStaff-1; i++);
    	{
    	   displayStaffDetails(i);
    	}
        }
     
        public static void addStaff(int MAX_STAFF)
        {
    	if (numStaff == MAX_STAFF)
    	{
    	    System.out.println("\n\tError: Maximum staff reached!");
    	    return;
    	}
    	char reply = 0;
    	do
    	{
    	    boolean found;
    	    int payNumber, position;
    	    do
    	    {
    		found = false;
    		System.out.print("\n\tEnter pay number: ");
    		payNumber = keyboard.nextInt();
    		position = findPayNum(payNumber);
    		if (position == -1)
    		{
    		    System.out.println("\n\tError: Invalid Pay Number!");
    		    found = true;
    		}
    	    }while (! found);
    	    payNum[numStaff] = payNumber;
    	    System.out.print("\n\tEnter surname: ");
    	    surname[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter first name: ");
    	    firstName[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter date of birth: ");
    	    dOB[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter department: ");
    	    dept[numStaff] = keyboard.next();
    	    numStaff++;
    	    System.out.println("\n\tSuccess: Staff details saved!");
    	    if (numStaff < MAX_STAFF)
    	    {
    		System.out.println("\n\tDo you want to add another staff member?");
    		System.out.print("\t(y/n): ");
    		reply = keyboard.next().charAt(0);
    	    }
    	}while ((reply != 'n') || (numStaff != MAX_STAFF));
        }
     
        public static void removeStaffMember()
        {
    	int payNumber, position;
    	if (numStaff == 0)
    	{
    	    System.out.println("\n\tError: No staff members!");
    	    return;
    	}
    	System.out.print("\n\tEnter pay number: ");
    	payNumber = keyboard.nextInt();
    	position = findPayNum(payNumber);
    	if (position < 0)
    	    System.out.println("\n\tError: Invalid pay number!");
    	else
    	    moveStaffData(position);
    	    System.out.println("Success: Staff member removed!");
        }
     
        public static void displayStaffMember()
        {
    	int payNumber, position;
    	System.out.print("\n\tEnter pay number: ");
    	payNumber = keyboard.nextInt();
    	position = findPayNum(payNumber);
    	if (position < 0)
    	    System.out.println("\n\tError: Invalid pay number!");
    	else
    	    System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
    	    System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
    	    displayStaffDetails(position);
        }
     
        public static void displayDeptStaff()
        {
    	String deptName;
    	System.out.print("\n\tEnter department name: ");
    	deptName = keyboard.next();
    	System.out.println("\n\n\tPay Num\t\tSurname\t\tFirst Name\t\tDoB\t\tDept");
    	System.out.println("\t-------\t\t-------\t\t----------\t\t---\t\t----\n");
    	for (int i=0; i<numStaff-1; i++);
    	{
    		if (dept[i] == deptName)
    		{
    		    displayStaffDetails(i);
    		}
    	}
        }
     
        public static void changeStaffDept()
        {
    	int payNumber, position;
    	String deptName;
    	System.out.print("\n\tEnter pay number: ");
    	payNumber = keyboard.nextInt();
    	position = findPayNum(payNumber);
    	if (position < 0)
    	{
    	    System.out.println("\n\tError: Invalid pay number!");
    	    return;
    	}
    	System.out.print("\n\tEnter department name: ");
    	deptName = keyboard.next();
    	dept[position] = deptName;
    	System.out.println("\n\tSuccess: Staff member department changed!");
        }
     
        public static void displayStaffDetails(int position)
        {
    	System.out.println("\n\n\t" + payNum[position] + "\t\t" + surname[position] + "\t\t" + firstName[position] + "\t\t" + dOB[position] + "\t\t" + dept[position]);
        }
     
        public static int findPayNum(int payNumber)
        {
    	if (numStaff == 0)
    	{
    	    System.out.println("\n\tError: No staff members!");
    	    return -1;
    	}
    	boolean found = false;
    	int count = 0;
    	do
    	{
    	    if (payNum[count] == payNumber)
    		found = true;
    	    else
    		count ++;
    	}while (! found | (count != numStaff));
    	if (! found)
    	{
    	    count = -1;
    	}
    	return count;
        }
     
        public static void moveStaffData(int startPos)
        {
    	for (int i=startPos; i<numStaff-2; i++);
    	{
    	    payNum[i] = payNum[i+1];
    	    surname[i] = surname[i+1];
    	    firstName[i] = firstName[i+1];
    	    dOB[i] = dOB[i+1];
    	    dept[i] = dept[i+1];
    	}
    	numStaff--;
        }
    }
    Attached Files Attached Files
    Last edited by madjavapuppy; August 6th, 2012 at 12:59 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    'java.lang.ArrayIndexOutOfBoundsException: 10
    that means the code is using an index greater than the size of the array.

    Look at the line where the exception occurred, see how many elements the array on that line has and see why the code used an index past the end of the array. If you can't see from the code, add a println that prints out the length of the array and the value of the index.

    Remember that arrays are zero based so the max index is the size of the array -1.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Many thanks for telling me how to format, I think I understand what your saying 'MAX_STAFF' is set to 10 and the 3 errors I have which are at

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    	at RefAssign1.findPayNum(RefAssign1.java:231)
    	at RefAssign1.addStaff(RefAssign1.java:121)
    	at RefAssign1.main(RefAssign1.java:40)

    have gone over 10 and cause the conflict, sorry though im still a bit confused, how specifically do I find the index value for these lines and how do I fix it so it they don't go over?
    Last edited by madjavapuppy; August 6th, 2012 at 01:20 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Please post the full text of the error message. It gives the line number where the exception occurred.
    if (payNum[count] == payNumber)
    If this is the statement,
    To see the values involved, add a println statement just before the statement where the error occurred and print out the length of the array (payNum.length) and the value of the index (count) used on the line where the error is.
    how do I fix it so it they don't go over
    Change the program's logic to keep the index's value within the bounds of the array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Ok this is what I got after doing it i used 'System.out.println(payNum.length + count);' which i hope was right.

    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    	at StaffManager.findPayNum(StaffManager.java:231)
    	at StaffManager.addStaff(StaffManager.java:121)
    	at StaffManager.main(StaffManager.java:40)

    So it goes to 10 then continues going after that, I still don't know how to make it so the index value stays within the bounds.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    What are those numbers from?
    Hint: When printing out a variable's value add an id String:
    System.out.println("payNum.length=" + payNum.length +", count="+count);

    What you printed was the value of this expression: (payNum.length + count) ==> the sum of the two numbers

    If the array has 10 elements, the max index is 9
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Oh... well i tried it that way and it came up as

    Exception in thread "main" payNum.length=10, count=0
    payNum.length=10, count=1
    payNum.length=10, count=2
    payNum.length=10, count=3
    payNum.length=10, count=4
    payNum.length=10, count=5
    payNum.length=10, count=6
    payNum.length=10, count=7
    payNum.length=10, count=8
    payNum.length=10, count=9
    payNum.length=10, count=10

    So payNum remains 10 and the count goes up to 10

    what needs to be done now?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    what needs to be done now?
    Keep count from going to 10.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    so would i do something like an if statement like

    if(count == 10)
    count -1;

    would that stop it?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    You need to look at the logic of the code where count and the array are used.
    What values should count have? What are they used for? When and why is the value of count changed?

    There is no reason that the value of count should be 10 if it is used to index an array of 10 elements.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Well the value of count is equal to the number of pay numbers 'payNumber' and is changed when the user adds or removes a staff member, so no it shouldn't be 10 it should be 7 at first.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Can you post the code that does what you just said?
    post#7 shows that count starts at 1 and goes to 10.

    You did not answer my questions in post#10:
    What values should count have?
    What are they used for?
    When and why is the value of count changed?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

     public static int findPayNum(int payNumber)
        {
    	if (numStaff == 0)
    	{
    	    System.out.println("\n\tError: No staff members!");
    	    return -1;
    	}
    	boolean found = false;
    	int count = 0;
    	do
    	{
    	    System.out.println("payNum.length=" + payNum.length +", count="+count);
    	    if (payNum[count] == payNumber)
     
    		found = true;
    	    else
    		count ++;
     
    	}while (! found || (count != numStaff));
    	if (! found)
    	{
    	    count = -1;
    	}
    	return count;
        }

    this method is used in another method further up called addStaff:

    public static void addStaff(int MAX_STAFF)
        {
    	if (numStaff == MAX_STAFF)
    	{
    	    System.out.println("\n\tError: Maximum staff reached!");
    	    return;
    	}
    	char reply = 0;
    	do
    	{
    	    boolean found;
    	    int payNumber, position;
    	    do
    	    {
    		found = false;
    		System.out.print("\n\tEnter pay number: ");
    		payNumber = keyboard.nextInt();
    		position = findPayNum(payNumber);
    		if (position == -1)
    		{
    		    System.out.println("\n\tError: Invalid Pay Number!");
    		    found = true;
    		}
    	    }while (! found);
    	    payNum[numStaff] = payNumber;
    	    System.out.print("\n\tEnter surname: ");
    	    surname[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter first name: ");
    	    firstName[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter date of birth: ");
    	    dOB[numStaff] = keyboard.next();
    	    System.out.print("\n\tEnter department: ");
    	    dept[numStaff] = keyboard.next();
    	    numStaff++;
    	    System.out.println("\n\tSuccess: Staff details saved!");
    	    if (numStaff < MAX_STAFF)
    	    {
    		System.out.println("\n\tDo you want to add another staff member?");
    		System.out.print("\t(y/n): ");
    		reply = keyboard.next().charAt(0);
    	    }
    	}while ((reply != 'n') || (numStaff != MAX_STAFF));
        }

    value of count is anywhere between 0-10 (because you can have no staff members), because I have 7 staff members already being used, their are 7 pay numbers which is the value of count at the beginning. Its used for the number of pay numbers being used, when a user goes into add staff or remove staff member, the count is either added or subracted depending on the option.
    Last edited by madjavapuppy; August 6th, 2012 at 02:52 PM.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    You did not answer my questions in post#10:
    What values should count have?
    What are they used for?
    When and why is the value of count changed?

    Can you explain what the code in post#13 does?
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Sorry obviously i don't understand the questions, 'findPayNumber' is used to keep track of the number of pay numbers and to find a pay number added in the method initaliseArrays:

     public static void initialiseArrays()
        {
    	int[] tempPayNum = {123456, 234567, 345678, 456789, 567890, 678901, 789012}; 
    	String[] tempSurname = {"Brown", "Jenkins", "Turner", "Cavendish", "Williams", "Ford", "Carter"};
    	String[] tempFirstName =  {"James", "Susan", "Jane", "Paul", "James", "Karen", "Daniel"};
    	String[] tempDOB =  {"24/3/85", "11/12/80", "5/7/72", "22/9/76", "19/1/81", "15/8/79", "4/2/62"};
    	String[] tempDept =  {"Marketing", "Sales", "Sales", "Accounts", "Marketing", "Sales", "Accounts"};

    'addStaff' is used for adding a new staff members, the user enters a new pay number, which initiates the method findPayNum, to make sure it does not conflict with an existing pay number then add 1 to the count, after that the user enters the other details such as the name and staff department.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    findPayNumber' is used ... to find a pay number
    Normally a search through an array is done with a for loop.
    Your use of a do{} while seems to be a problem.

    Here are the answers to my questions:
    In the loop doing the search, what is the variable count used for? It looks like it is the index to an array.
    What values could count have? 0 to the number of entries - 1 as the search looks in the array for the match
    The value of count is incremented by one when the current element does not match.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    i don't how im going to use a for loop in this, is there no way to make it work with a do{} while?

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    You use a for loop to look at elements in an array one at a time starting with the first one. The loop can stop when a match is found or when there are no more elements in the array.
    Does that sound like what your method is doing?

    Yes you can make it work with any type of loop, but the for loop is the natural one to use.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    That's true, so how would i use the for loop in this situation?

    looking at it guessed it would start with 'for (int i=payNumber; i<count-1; i++);' is that right?
    Last edited by madjavapuppy; August 6th, 2012 at 03:35 PM.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Like I said in post#10.
    If you have problems coding a for loop:
    http://docs.oracle.com/javase/tutori...bolts/for.html
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    madjavapuppy (August 6th, 2012)

  22. #21
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    wow thank you so much that seems to have worked i replaced it with:

     public static int findPayNum(int payNumber)
        {
    	if (numStaff == 0)
    	{
    	    System.out.println("\n\tError: No staff members!");
    	    return -1;
    	}
    	boolean found = false;
    	for (int i=0; i<11; i++);
    	{
    	    if(payNum[i] == payNumber)
    		found = true;
    	    else
    		i ++;
    	}
    	if(! found)
    	{
    	    i = -1;
    	}
    	return i;
        }
    Last edited by madjavapuppy; August 6th, 2012 at 03:57 PM.

  23. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    for (int i=0; i<11; i++);
    That statement has two problems:
    1) you should use a variable not a hardcoded 11 for testing the end of the looping
    2) the ending ; ends the for statement. The next statement(s) is NOT in the for loop


    Have you tested the method? Does it return the correct value?
    If you don't understand my answer, don't ignore it, ask a question.

  24. #23
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Im not sure if its working now actually, what i put in says its an invalid pay number, but lets me put the rest of the details in and says it a success, then asks if I want to put another one in, regardless of me putting yes or no it repeats the addStaff method.

    Right ok so when I do it again after the second time it comes back with the same error as before except this time it is -1. Which must have happened because of:

    if(! found)
    	{
    	    i = -1;
    	}

    So as well as this how do i fix it and the for statement?
    Last edited by madjavapuppy; August 6th, 2012 at 04:21 PM.

  25. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    The posted code should not work. After making the two changes I suggested, test it with good and bad data to see if it works.
    If you don't understand my answer, don't ignore it, ask a question.

  26. #25
    Junior Member
    Join Date
    Aug 2012
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: java.lang.ArrayIndexOutOfBoundsException help

    Ok made the changes, made an 'int count = 10;' for loop changed to 'for (int i=0; i<count; i++)' tried 11, but it caused an error after pay number is entered the test you showed came with payNum.length = 10, count = 10. It says Invalid Pay Number an error I put in addStaff(post 13) and it repeats the method even when I put n(no) when it asks do I wish to add another staff member(also in addStaff post 13). Also changed the 'i = -1;' to 'i --;'.

Page 1 of 2 12 LastLast

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException
    By rajesh3006 in forum Member Introductions
    Replies: 1
    Last Post: March 12th, 2012, 07:32 AM
  2. java.lang.ArrayIndexOutOfBoundsException - huh?
    By mrUnknown in forum Exceptions
    Replies: 3
    Last Post: February 18th, 2012, 09:06 AM
  3. [SOLVED] java.lang.ArrayIndexOutOfBoundsException
    By anmaston in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 12th, 2011, 04:43 PM
  4. ArrayIndexOutOfBoundsException
    By NightFire91 in forum Exceptions
    Replies: 1
    Last Post: October 31st, 2010, 06:06 AM
  5. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM