-
1 Attachment(s)
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?
Code java:
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--;
}
}
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Quote:
'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.
-
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
Code java:
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?
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Please post the full text of the error message. It gives the line number where the exception occurred.
Quote:
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.
Quote:
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.
-
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.
Code java:
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. :(
-
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
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Oh... well i tried it that way and it came up as
Code java:
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?
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Quote:
what needs to be done now?
Keep count from going to 10.
-
Re: java.lang.ArrayIndexOutOfBoundsException help
so would i do something like an if statement like
if(count == 10)
count -1;
would that stop it?
-
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.
-
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.
-
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?
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Code java:
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:
Code java:
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.
-
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?
-
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:
Code java:
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.
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Quote:
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.
-
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?
-
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.
-
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?
-
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
-
Re: java.lang.ArrayIndexOutOfBoundsException help
wow thank you so much that seems to have worked i replaced it with:
Code java:
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;
}
-
Re: java.lang.ArrayIndexOutOfBoundsException help
Code :
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?
-
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:
So as well as this how do i fix it and the for statement?
-
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.
-
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 --;'.