错误:线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2

时间:2017-02-12 15:05:09

标签: java arrays

如何解决此错误

线程“main”中的异常java.lang.ArrayIndexOutOfBoundsException:2

当我尝试运行此程序时,我收到此错误消息。 真的陷入了这个程序

public static void main(String[] args)
{
    String firstLine = (args[0]);
    String secondLine = (args[1]);
    String thirdLine = (args[2]);
    int LinesBefore = Integer.parseInt(args[3]);
    int LinesAfter = Integer.parseInt(args[4]);
    int Spaces = Integer.parseInt(args[5]);
    if (args.length == 1 || args.length == 2 || args.length == 5 || args.length > 6)
    {
        Open open = new Open();
        System.out.println("Error: Incorrect numbers of parameter.");
        System.out.println("Program now terminating.");
    }
    else if(args.length == 3)
    {
        Open open = new Open(firstLine,secondLine,thirdLine);
        open.display();
    }
    else if(args.length == 4)
    {
        Open open= new Open(firstLine,secondLine,thirdLine,LinesBefore);
        open.display();
    }
    else if(args.length == 6)
    {
        Open open = new Open(firstLine,secondLine,thirdLine,LinesBefore,LinesAfter,Spaces);
        open.display();        }
    else if(args.length == 0)
    {
        Open open = new Open();
        open.display();        }
}

3 个答案:

答案 0 :(得分:1)

您的错误在这里:

String firstLine = (args[0]);
String secondLine = (args[1]);
String thirdLine = (args[2]);
int LinesBefore = Integer.parseInt(args[3]);
int LinesAfter = Integer.parseInt(args[4]);
int Spaces = Integer.parseInt(args[5]);

您尝试访问数组中可能不存在的元素,具体取决于您在运行时指定的参数数量。

可能的解决方案之一:

public static void main(String[] args)
{
    if (args.length == 1 || args.length == 2 || args.length == 5 || args.length > 6)
    {
        Open open = new Open();
        System.out.println("Error: Incorrect numbers of parameter.");
        System.out.println("Program now terminating.");
    }
    else if(args.length == 3)
    {
        Open open = new Open(args[0],args[1],args[2]);
        open.display();
    }
    else if(args.length == 4)
    {
        Open open= new Open(args[0],args[1],args[2],Integer.parseInt(args[3]));
        open.display();
    }
    else if(args.length == 6)
    {
        Open open = new Open(args[0],args[1],args[2],Integer.parseInt(args[3]),Integer.parseInt(args[4]),Integer.parseInt(args[5]));
        open.display();        }
    else if(args.length == 0)
    {
        Open open = new Open();
        open.display();        }
}

答案 1 :(得分:1)

如果args[0]等,您只能使用args.length > 0

我建议在条件内移动args的访问权限,而不是在args.length检查之前。例如。

else if(args.length == 3)
{
   String firstLine = (args[0]);
   String secondLine = (args[1]);
   String thirdLine = (args[2]);

   Open open = new Open(firstLine,secondLine,thirdLine);
   open.display();
}

答案 2 :(得分:0)

String firstLine = (args[0]);
String secondLine = (args[1]);
String thirdLine = (args[2]);
int LinesBefore = Integer.parseInt(args[3]);
int LinesAfter = Integer.parseInt(args[4]);
int Spaces = Integer.parseInt(args[5]);

要使其工作args.length必须至少为6,否则您无法访问最多5个所有数组索引。要避免此类异常,您必须先检查args.length和然后永远不要访问大于array.length - 1

的索引