如何删除“;java.lang.ArrayIndexOutOfBoundsException“;不使用try-and-catch时出错? import java.util.Scanner; 公营小型大学计划{ 公共静态void main(字符串[]args){ int i=0; 扫描仪sc=新的扫描仪(System.in); 国际环境管理局; 字符串employee_info[][]= { {“1001”、“Ashish”、“01/04/2009”、“e”、“研发”、“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”,“前台”,“12000”,“6000”,“2000”}, {“1005”、“Ranjan”、“16/07/2005”、“m”、“Engg”、“50000”、“20000”、“20000”}, {“1006”、“Suman”、“1/1/2000”、“e”、“制造”、“23000”、“9000”、“4400”}, {“1007”、“Tanmay”、“12/06/2006”、“c”、“PM”、“29000”、“12000”、“10000”} }; 字符串DA[][]= { {“e”,“工程师”,“20000”}, {“c”,“顾问”,“32000”}, {“k”,“办事员”,“12000”}, {“r”,“接待员”,“15000”}, {“m”,“经理”,“40000”} }; System.out.println(“输入员工编号”); emp_no=sc.nextInt(); 对于(i=0;i

如何删除“;java.lang.ArrayIndexOutOfBoundsException“;不使用try-and-catch时出错? import java.util.Scanner; 公营小型大学计划{ 公共静态void main(字符串[]args){ int i=0; 扫描仪sc=新的扫描仪(System.in); 国际环境管理局; 字符串employee_info[][]= { {“1001”、“Ashish”、“01/04/2009”、“e”、“研发”、“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”,“前台”,“12000”,“6000”,“2000”}, {“1005”、“Ranjan”、“16/07/2005”、“m”、“Engg”、“50000”、“20000”、“20000”}, {“1006”、“Suman”、“1/1/2000”、“e”、“制造”、“23000”、“9000”、“4400”}, {“1007”、“Tanmay”、“12/06/2006”、“c”、“PM”、“29000”、“12000”、“10000”} }; 字符串DA[][]= { {“e”,“工程师”,“20000”}, {“c”,“顾问”,“32000”}, {“k”,“办事员”,“12000”}, {“r”,“接待员”,“15000”}, {“m”,“经理”,“40000”} }; System.out.println(“输入员工编号”); emp_no=sc.nextInt(); 对于(i=0;i,java,Java,我想从输出中删除错误,只打印“没有emp id的员工:”这一条。现在它正在打印输出和错误。如果数组employee_info中不存在输入的emp no,如何在不打印语句的情况下删除错误 如果可以在不使用try-and-catch块的情况下删除错误,那么如何执行此操作。一旦您在“输入员工编号”之后退出循环,并在下一行使用I(循环完成后,该值将为7),将引发越界异常。试试这个: import java.util.Scanner; public class Mini_Project { pu

我想从输出中删除错误,只打印“没有emp id的员工:”这一条。现在它正在打印输出和错误。如果数组employee_info中不存在输入的emp no,如何在不打印语句的情况下删除错误


如果可以在不使用try-and-catch块的情况下删除错误,那么如何执行此操作。

一旦您在“输入员工编号”之后退出循环,并在下一行使用
I
(循环完成后,该值将为7),将引发越界异常。试试这个:

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();
    }
}
System.out.println(“输入员工编号”);
emp_no=sc.nextInt();
字符串[]emp=null;
对于(i=0;i
IndexOOBE
在尝试访问超出范围的数组中的索引时发生。例如,如果分配一个大小为10的数组,则可以访问索引0到9之间的元素(包括0和9)。除此之外的任何索引都将为您提供
IOOBException

在您的情况下,当您迭代并没有找到员工时,
i
的值为7。但正如我上面解释的,你的例子中的范围是0到6。因此,当您访问索引7时,您会得到
OutOfBoundException

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

以下是更改后的代码:

    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];
    }
public class Mini_项目{
公共静态void main(字符串[]args){
int i=0;
扫描仪sc=新的扫描仪(System.in);
国际环境管理局;
字符串employee_info[][]={{“1001”、“Ashish”、“01/04/2009”、“e”、“R&D”、“20000”、“800”
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();
}
}