如何在不使用 try and catch 的情况下删除"java.lang.ArrayIndexOutOfBoundsException"错误?


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.ttEmp NamettDepartmentttDesignationttSalary");
System.out.println(emp_no+"tt"+emp_name +"ttt"+emp_dept+"ttt"+emp_designation+"tt"+emp_salary);
}
if(emp_no == 1003 || emp_no == 1005)
{
System.out.println("Emp No.ttEmp NamettDepartmentttDesignationttSalary");  System.out.println(emp_no+"tt"+emp_name+"ttt"+emp_dept+"ttt"+emp_designation+"ttt"+emp_salary);
}
if(emp_no == 1004 || emp_no == 1006)
{
System.out.println("Emp No.ttEmp NamettDepartmentttDesignationttSalary");
System.out.println(emp_no+"tt"+emp_name +"ttt"+emp_dept+"tt"+emp_designation+"tt"+emp_salary);
}
sc.close();
}
}

我想从输出中删除错误,并想打印"没有员工具有emp id:"仅此。现在它正在打印输出和错误。如果数组employee_info中不存在未输入的emp,如何在不仅打印语句的情况下删除错误?

如果可以在不使用 try and catch 块的情况下删除错误,那么我该怎么做。

当您尝试访问超出范围的数组中的索引时,会发生IndexOOBE。例如,如果分配大小为 10 的数组,则可以访问索引 0 到 9(包括 9(之间的元素。除此之外的任何索引都会给你IOOBException.

在您的情况下,当您迭代但找不到员工时,i的值将为 7。但正如我上面解释的,您在这种情况下的范围是 0 到 6。因此,当您访问索引 7 时,您会得到OutOfBoundException

您的代码可以在很多方面进行改进。为了简单起见,为了解决当前的问题,您可以使用employeeExists标志来指示该员工是否存在,然后对该员工进行操作。

以下是更改后的代码:

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.ttEmp NamettDepartmentttDesignationttSalary");
System.out.println(emp_no + "tt" + emp_name + "ttt" + emp_dept + "ttt" + emp_designation
+ "tt" + emp_salary);
}
if (emp_no == 1003 || emp_no == 1005) {
System.out.println("Emp No.ttEmp NamettDepartmentttDesignationttSalary");
System.out.println(emp_no + "tt" + emp_name + "ttt" + emp_dept + "ttt" + emp_designation
+ "ttt" + emp_salary);
}
if (emp_no == 1004 || emp_no == 1006) {
System.out.println("Emp No.ttEmp NamettDepartmentttDesignationttSalary");
System.out.println(emp_no + "tt" + emp_name + "ttt" + emp_dept + "tt" + emp_designation + "tt"
+ emp_salary);
}
}
sc.close();
}
}

一旦您在"输入员工编号"之后退出循环并在下一行使用i,就会引发越界异常(循环完成后,该值将值为 7,如此处所述(。试试这个:

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];
}

相关内容

最新更新