-2
import java.util.Scanner;

public class Mini_Project {
    public static void main(String[] args) {
        int i = 0;
        Scanner sc = new Scanner(System.in);
        int emp_no;
        String employee_info[][] = 
        {
            {"1001", "Ashish", "01/04/2009", "e", "R&D", "20000", "8000", "3000"},
            {"1002", "Sushma", "23/08/2012", "c", "PM", "30000", "12000", "9000"},
            {"1003", "Rahul", "12/11/2008", "k", "Acct", "10000", "8000", "1000"},
            {"1004", "Chahat", "29/01/2013", "r", "Front Desk", "12000", "6000", "2000"},
            {"1005", "Ranjan", "16/07/2005", "m", "Engg", "50000", "20000", "20000"},
            {"1006", "Suman", "1/1/2000", "e", "Manufacturing", "23000", "9000", "4400"},
            {"1007", "Tanmay", "12/06/2006", "c", "PM", "29000", "12000", "10000"}
        };

        String DA[][] = 
        {
            {"e", "Engineer", "20000"},
            {"c", "Consultant", "32000"},
            {"k", "Clerk", "12000"},
            {"r", "Receptionist", "15000"},
            {"m", "Manager", "40000"}
        };  

        System.out.println("Enter the employee number.");
        emp_no = sc.nextInt();

        for(i = 0; i < employee_info.length; i++)
        {
            if(emp_no == Integer.parseInt(employee_info[i][0]))
            {
                emp_no = Integer.parseInt(employee_info[i][0]);
                break;
            }
            if(i == 6)
            {
                System.out.println("There is no employee with emp id : " + emp_no);
            }
        }

        String emp_name = employee_info[i][1];
        String emp_dept = employee_info[i][4];
        char emp_designation_code = employee_info[i][3].charAt(0);
        String emp_designation = "NULL";
        int emp_salary = 0;
        int basic = Integer.parseInt(employee_info[i][5]);
        int hra = Integer.parseInt(employee_info[i][6]);
        int it = Integer.parseInt(employee_info[i][7]);

        switch(emp_designation_code)
        {
            case 'e': emp_designation = DA[0][1];
                      emp_salary = basic + hra + Integer.parseInt(DA[0][2]) - it;
                      break;

            case 'c': emp_designation = DA[1][1];
                      emp_salary = basic + hra + Integer.parseInt(DA[1][2]) - it;
                      break;

            case 'k': emp_designation = DA[2][1];
                      emp_salary = basic + hra + Integer.parseInt(DA[2][2]) - it;
                      break;

            case 'r': emp_designation = DA[3][1];
                      emp_salary = basic + hra + Integer.parseInt(DA[3][2]) - it;
                      break;

            case 'm': emp_designation = DA[4][1];
                      emp_salary = basic + hra + Integer.parseInt(DA[4][2]) - it;
                      break;
        }

        if(emp_no == 1001 || emp_no== 1002 ||emp_no == 1007)
        {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");
            System.out.println(emp_no+"\t\t"+emp_name +"\t\t\t"+emp_dept+"\t\t\t"+emp_designation+"\t\t"+emp_salary);
        }

        if(emp_no == 1003 || emp_no == 1005)
        {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");  System.out.println(emp_no+"\t\t"+emp_name+"\t\t\t"+emp_dept+"\t\t\t"+emp_designation+"\t\t\t"+emp_salary);
        }

        if(emp_no == 1004 || emp_no == 1006)
        {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");
            System.out.println(emp_no+"\t\t"+emp_name +"\t\t\t"+emp_dept+"\t\t"+emp_designation+"\t\t"+emp_salary);
        }

        sc.close();
    }
}

I want to remove the error from the output and want to print "There is no employee with emp id:" this only. Right now it is printing both output and error. How can I remove the error without to print only the statement if the emp no entered is not present in the array employee_info?

If it is possible to remove the error without using try and catch block then how can I do so.

Savior
  • 3,225
  • 4
  • 24
  • 48
  • 1
    Check if the index you're about to access is within bounds (that is 0 <= index < length) before you index into the array. – Mark Rotteveel May 22 '20 at 16:13
  • 1
    Hey! You can remove it by programming code that doesn't throw it. That's the best way, I promise. – akuzminykh May 22 '20 at 16:20
  • 2
    Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Savior May 22 '20 at 16:34

2 Answers2

1

The out of bounds exception is thrown once you exit the loop after "Enter employee number" and use i on the next line (which will have value 7 once the loop finishes as explained here). Try this:

    System.out.println("Enter the employee number.");
    emp_no = sc.nextInt();

    String[] emp = null;

    for(i = 0; i < employee_info.length; i++)
    {
        if(emp_no == Integer.parseInt(employee_info[i][0]))
        {
            emp = employee_info[i];
            break;
        }
    }

    if (emp == null)
    {
        System.out.println("There is no employee with emp id : " + emp_no);
    }
    else 
    {
        String emp_no = emp[0];           
        String emp_name = emp[1];
        String emp_dept = emp[4];
    }
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
1

IndexOOBE occurs when you try to access an index in an array which is out of range. For example, if you allocate an array of size 10, you can access the elements between index 0 to 9(both inclusive). any index other than that is going to give you IOOBException.

In your case, when you iterate and don't find the employee, i will have the value 7. But as I explained above, the range in your case is 0 to 6. So you get OutOfBoundException when you access index 7.

Your code can be improved in a lot of areas. Just to keep it simple and just to solve your current issue, you can use a employeeExists flag to indicate if that employee exists and then do your operations on that employee.

Following is the altered code:

public class Mini_Project {
public static void main(String[] args) {
    int i = 0;
    Scanner sc = new Scanner(System.in);
    int emp_no;
    String employee_info[][] = { { "1001", "Ashish", "01/04/2009", "e", "R&D", "20000", "8000", "3000" },
            { "1002", "Sushma", "23/08/2012", "c", "PM", "30000", "12000", "9000" },
            { "1003", "Rahul", "12/11/2008", "k", "Acct", "10000", "8000", "1000" },
            { "1004", "Chahat", "29/01/2013", "r", "Front Desk", "12000", "6000", "2000" },
            { "1005", "Ranjan", "16/07/2005", "m", "Engg", "50000", "20000", "20000" },
            { "1006", "Suman", "1/1/2000", "e", "Manufacturing", "23000", "9000", "4400" },
            { "1007", "Tanmay", "12/06/2006", "c", "PM", "29000", "12000", "10000" } };

    String DA[][] = { { "e", "Engineer", "20000" }, { "c", "Consultant", "32000" }, { "k", "Clerk", "12000" },
            { "r", "Receptionist", "15000" }, { "m", "Manager", "40000" } };

    System.out.println("Enter the employee number.");
    emp_no = sc.nextInt();

    boolean employeeExists = false;
    for (i = 0; i < employee_info.length; i++) {
        if (emp_no == Integer.parseInt(employee_info[i][0])) {
            emp_no = Integer.parseInt(employee_info[i][0]);
            employeeExists = true;
            break;
        }
        if (i == 6) {
            System.out.println("There is no employee with emp id : " + emp_no);
        }
    }

    if (employeeExists) {
        String emp_name = employee_info[i][1];
        String emp_dept = employee_info[i][4];
        char emp_designation_code = employee_info[i][3].charAt(0);
        String emp_designation = "NULL";
        int emp_salary = 0;
        int basic = Integer.parseInt(employee_info[i][5]);
        int hra = Integer.parseInt(employee_info[i][6]);
        int it = Integer.parseInt(employee_info[i][7]);

        switch (emp_designation_code) {
        case 'e':
            emp_designation = DA[0][1];
            emp_salary = basic + hra + Integer.parseInt(DA[0][2]) - it;
            break;

        case 'c':
            emp_designation = DA[1][1];
            emp_salary = basic + hra + Integer.parseInt(DA[1][2]) - it;
            break;

        case 'k':
            emp_designation = DA[2][1];
            emp_salary = basic + hra + Integer.parseInt(DA[2][2]) - it;
            break;

        case 'r':
            emp_designation = DA[3][1];
            emp_salary = basic + hra + Integer.parseInt(DA[3][2]) - it;
            break;

        case 'm':
            emp_designation = DA[4][1];
            emp_salary = basic + hra + Integer.parseInt(DA[4][2]) - it;
            break;
        }

        if (emp_no == 1001 || emp_no == 1002 || emp_no == 1007) {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");
            System.out.println(emp_no + "\t\t" + emp_name + "\t\t\t" + emp_dept + "\t\t\t" + emp_designation
                    + "\t\t" + emp_salary);
        }

        if (emp_no == 1003 || emp_no == 1005) {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");
            System.out.println(emp_no + "\t\t" + emp_name + "\t\t\t" + emp_dept + "\t\t\t" + emp_designation
                    + "\t\t\t" + emp_salary);
        }

        if (emp_no == 1004 || emp_no == 1006) {
            System.out.println("Emp No.\t\tEmp Name\t\tDepartment\t\tDesignation\t\tSalary");
            System.out.println(emp_no + "\t\t" + emp_name + "\t\t\t" + emp_dept + "\t\t" + emp_designation + "\t\t"
                    + emp_salary);
        }
    }

    sc.close();
}
}
Arun Gowda
  • 2,721
  • 5
  • 29
  • 50
  • You're not adding anything above/beyond what Mark Rotteveel and akuzminykh already said above: `Hey! You can remove it by programming code that doesn't throw it. That's the best way, I promise.` – FoggyDay May 23 '20 at 05:43
  • True @FoggyDay. But does that explain the "HOW" part or the "WHY" part? OP does not know why & where exactly its going wrong. "Hey don't run into this problem / avoid this problem" isn't the solution to a problem. Is it? – Arun Gowda Jul 04 '20 at 12:49