-
Out of Bounds Exception
I know it seems like a pretty basic error, but I cant for the life of me, figure out the problem I have with my code
Its supposed to read how many 'employees' the user wants to input, create space for it, and label it maxEmployees. The only issue im having so far is with the printEmployee method where it throws me an out of bounds exception error
(Sorry bout the length, if it helps, its the method directly at the bottom)
Error: ( used 3 as maxEmployees)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Database.printEmployee<Database.java:168>
at Database.menu<Database.java:91>
at Database.main<Database.java:43>
Here is the code:
import java.util.Scanner;
public class Database
{
static int[] empNumber;
static String[] empName;
static String[] empAddress;
static int[] empSalary;
static Scanner scan= new Scanner(System.in);
static int maxEmployees = 0;
static int empID = 0;
static Database db= new Database();
Database addEmployee, deleteEmployee, printEmployee, printEmployeeList;
public static void main (String [] args)
{
System.out.println("How many employees would you like to store?");
int numOfEmployees = scan.nextInt();
scan.nextLine();
db.menu(numOfEmployees);
}
public void menu(int numOfEmployees)
{
maxEmployees=numOfEmployees;
empNumber= new int [maxEmployees];
empName= new String [maxEmployees];
empAddress= new String [maxEmployees];
empSalary= new int [maxEmployees];
String choice = " ";
boolean doAgain = true;
int count;
while(doAgain==true)
{
System.out.println("");
System.out.println("Employee Management System Menu");
System.out.println("a) Add a new Employee");
System.out.println("b) Delete an Employee");
System.out.println("c) Print the Employee List");
System.out.println("d) Print a Specific Employee");
System.out.println("e) Exit the Employee Management System");
System.out.println("");
System.out.println("Please input the letter choice.");
choice = scan.nextLine();
System.out.println("");
if (choice.equalsIgnoreCase("a"))
db.addEmployee();
else if(choice.equalsIgnoreCase("b"))
{
System.out.println("Please input the Employee Number.");
empID = scan.nextInt();
scan.nextLine();
db.deleteEmployee(empID);
}
else if(choice.equalsIgnoreCase("c"))
db.printEmployeeList();
else if(choice.equalsIgnoreCase("d"))
{
System.out.println("Please input the Employee Number.");
empID = scan.nextInt();
scan.nextLine();
db.printEmployee(empID);
}
else if(choice.equalsIgnoreCase("e"))
doAgain=false;
}
}
public void addEmployee()
{
int count = 0;
int value;
int loop = 0;
while (empNumber[count] != 0)
count++;
System.out.println("Please input Employee's Number");
value = scan.nextInt();
scan.nextLine();
System.out.println("");
while((loop != maxEmployees) && (value != empNumber[loop]))
loop++;
if(loop==maxEmployees)
{
empNumber[count] = value;
System.out.println("Please input Employee's Name");
empName[count]= scan.nextLine();
System.out.println("");
System.out.println("Please input Employee's Address");
empAddress[count]= scan.nextLine();
System.out.println("");
System.out.println("Please input Employee's Salary");
empSalary[count]= scan.nextInt();
scan.nextLine();
System.out.println("");
}
else if(loop!=maxEmployees)
System.out.println("Employee number already exists.");
}
public void deleteEmployee(int empID)
{
int count = 0;
while (empNumber[count] != empID)
count++;
empName[count] = null;
empAddress[count] = null;
empSalary[count]=0;
empNumber[count]=0;
}
public void printEmployeeList()
{
int count = 0;
while (count != maxEmployees)
{
if(empNumber[count] != 0)
{
System.out.println("Employee Number: " +empNumber[count]);
System.out.println("Employee Name: " +empName[count]);
System.out.println("Employee Address: " +empAddress[count]);
System.out.println("Employee Salary: " +empSalary[count]);
System.out.println("");
}
count++;
}
}
public void printEmployee(int empID)
{
int count = 0;
while ((empNumber[count] != empID) || (count != maxEmployees))
count++;
if(empNumber[count] == empID)
{
System.out.println("Employee Number: " +empNumber[empID]);
System.out.println("Employee Name: " +empName[empID]);
System.out.println("Employee Address: " +empAddress[empID]);
System.out.println("Employee Salary: " +empSalary[empID]);
System.out.println("");
}
else if(count == maxEmployees)
System.out.println("Employee Number (" +empID+ ") does not exist.");
}
}
-
Never mind haha
Realized the problem.