Results 1 to 2 of 2
  1. #1
    johnwy8 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default 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.");
    }
    }

  2. #2
    johnwy8 is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

Similar Threads

  1. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  2. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  3. Array out of bounds exception 20.
    By dropt in forum New To Java
    Replies: 4
    Last Post: 09-21-2009, 10:32 PM
  4. Java ArrayList out of bounds exception
    By grahamb314 in forum New To Java
    Replies: 5
    Last Post: 11-22-2008, 07:21 PM
  5. [SOLVED] out of bounds exception help
    By soxfan714 in forum New To Java
    Replies: 21
    Last Post: 11-11-2008, 08:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •