如何删除 "java.lang.ArrayIndexOutOfBoundsException "错误而不使用try和catch?[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何删除 "java.lang.ArrayIndexOutOfBoundsException "错误而不使用try和catch?[重复]相关的知识,希望对你有一定的参考价值。

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.		Emp Name		Department		Designation		Salary");
            System.out.println(emp_no+"		"+emp_name +"			"+emp_dept+"			"+emp_designation+"		"+emp_salary);
        }

        if(emp_no == 1003 || emp_no == 1005)
        {
            System.out.println("Emp No.		Emp Name		Department		Designation		Salary");  System.out.println(emp_no+"		"+emp_name+"			"+emp_dept+"			"+emp_designation+"			"+emp_salary);
        }

        if(emp_no == 1004 || emp_no == 1006)
        {
            System.out.println("Emp No.		Emp Name		Department		Designation		Salary");
            System.out.println(emp_no+"		"+emp_name +"			"+emp_dept+"		"+emp_designation+"		"+emp_salary);
        }

        sc.close();
    }
}

我想从输出中去掉错误,想只打印 "There is no employee with emp id: "这个。现在它同时打印输出和错误。如果输入的emp no没有出现在数组employee_info中,我怎样才能不只打印语句来删除错误?

如果可以不使用try和catch块来删除错误,那么我可以怎么做。

答案

IndexOOBE 当你试图访问一个数组中超出范围的索引时,就会发生这种情况。例如,如果你分配了一个大小为10的数组,你可以访问索引0到9之间的元素(包括两者)。IOOBException.

在你的情况下,当你迭代而找不到雇员。i 会有7的值。但正如我上面所解释的,在你的情况下,范围是0到6。所以你得到的是 OutOfBoundException 当你访问索引7时。

你的代码可以在很多方面进行改进。为了简单起见,也为了解决你目前的问题,你可以使用一个叫做 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.		Emp Name		Department		Designation		Salary");
            System.out.println(emp_no + "		" + emp_name + "			" + emp_dept + "			" + emp_designation
                    + "		" + emp_salary);
        }

        if (emp_no == 1003 || emp_no == 1005) {
            System.out.println("Emp No.		Emp Name		Department		Designation		Salary");
            System.out.println(emp_no + "		" + emp_name + "			" + emp_dept + "			" + emp_designation
                    + "			" + emp_salary);
        }

        if (emp_no == 1004 || emp_no == 1006) {
            System.out.println("Emp No.		Emp Name		Department		Designation		Salary");
            System.out.println(emp_no + "		" + emp_name + "			" + emp_dept + "		" + emp_designation + "		"
                    + 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];
    }

以上是关于如何删除 "java.lang.ArrayIndexOutOfBoundsException "错误而不使用try和catch?[重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Linux 中删除超大的文件

如何在list 中删除指定元素

如何在list中删除我指定的对象

java如何删除掉数组中的某个元素??

java如何删除掉数组中的某个元素??

fineui 的grid 中如何删除勾选的项