由于解析int,我得到一个ArrayIndexOutOfBoundsException错误。但我需要解析那个int?

时间:2016-11-21 10:30:07

标签: java indexoutofboundsexception

程序需要在没有参数和参数的情况下执行。程序根据命令行的输入执行不同的操作。这是代码,第94行导致我解析int的问题(int a = Integer.parseInt(args [0]);)

但我必须解析那个int才能用五个或更多命令行参数执行程序。

这是代码,很长,但问题出在第94行:

class CommandArgsOrNot{
public static void main(String[] args) {
    final int clargs = args.length; 
        if (clargs==0) {
            System.out.print(" "); 
            System.out.println("Hello"); 
            for (int i = 0; i < 3; i++) {
                System.out.print(" "); 
                }
            System.out.println("World !!"); 
        }

        if (clargs==1) {
            String s = args[0];
            for (int i = 0; i < s.length(); i++) {
                for (int j = 0; j < i; j++) {
                    System.out.print(" "); 
                }
            System.out.println(s); 
            }
        }

        if (clargs==2) {
            System.out.println("ARGUMENT 1:" + " " + (args[0])); 
            System.out.println("ARGUMENT 2:" + " " + (args[1])); 
        }

        if (clargs==3) {
        final int a = Integer.parseInt(args[0]);
        final int b = Integer.parseInt(args[1]);
        final int c = Integer.parseInt(args[2]); 

        if (a * b == c) {
            System.out.println("1*2=3"); 
        }
        else if (a * c == b) {
        System.out.println("1*3=2");
        }
        else if (b * a == c) {
        System.out.println("2*1=3");
        }
        else if (b * c == a) {
        System.out.println("2*3=1");
        }
        else if (c * a == b) {
        System.out.println("3*1=2"); 
        }
        else if (c * b == a) {
        System.out.println("3*2=1");
        }
        else {
        System.out.println("None"); 
        }
        System.out.println(); 
        }

        if (args.length == 4) {
        int a = Integer.parseInt(args[0]);
        int b = Integer.parseInt(args[1]); 
        int c = Integer.parseInt(args[2]);
        int d = Integer.parseInt(args[3]);  
        if (a == b && a == c && a == d) {
            System.out.println("1"); 
        }
        else if (a != b && b != c && c != d) {
            System.out.println("4");
        }
        else if (a == b) {
            System.out.println("2");
        }
        else if (c == d && a != b) {
            System.out.println("3");
        }
        else if (b == c) {
            System.out.println("2");
        }
        else if (a == b && a == c) {
            System.out.println("3");
        }
        else if (d == c && d == b) {
            System.out.println("3");
        }
        else if (a == d) {
            System.out.println("2");
        }
        else if (a == c && b == d) {
            System.out.println("2");
        }
        else if (a == d && b != c) {
        System.out.println("3");
        }
        }

        int a = Integer.parseInt(args[0]);
        long answer = 1;
        long result = 0; 
        if (a < 0) {
            for(int i = 1; i < args.length; i++) {
                answer *=  Integer.parseInt(args[i]);
            }
            System.out.println(answer);
        }
        else if (a == 0) {
            for(int i = 0; i < args.length; i++) {
                result += Integer.parseInt(args[i]);
            }
            System.out.println(result); 
        }
        else if (a == 1) {
            for(int i = 0; i < args.length; i++) {
                result += Integer.parseInt(args[i]);
            }
            System.out.println(result); 
        }
        else if (a == 2) {
            for(int i = 2; i < args.length; i++) {
                if (i % 2 != 0){
                answer += Integer.parseInt(args[i]); 
                } 

            }
            System.out.println(answer); 
        }
        else if (a == 3) {
            for (int i = 2; i < args.length; i++) {
                if (i % 3 != 0) {
                answer += Integer.parseInt(args[i]);
                }
            }
            System.out.println(answer); 
        } 
}
}

非常感谢任何帮助,谢谢!

4 个答案:

答案 0 :(得分:0)

如果您没有将任何参数传递到return计划,则应使用Java个关键字停止该计划。

例如:

 if (clargs==0) {
            System.out.print(" "); 
            System.out.println("Hello"); 
            for (int i = 0; i < 3; i++) {
                System.out.print(" "); 
                }
            System.out.println("World !!"); 

            return;
        } //please note that there is also many alternative ways to control your program.

但是,主要的想法是,如果args[..]值等于0,则不应将任何其他int值解析为agrs[..]

答案 1 :(得分:0)

我相信你的输出是 -

 Hello
   World !!

之后你就出错了。 这是因为你已经访问int a = Integer.parseInt(args[0])第0个参数而没有检查它是否存在,如果没有提供任何参数,那么它会抛出错误。

在访问之前检查存在。

答案 2 :(得分:0)

访问数组元素时,其索引确保以下内容。

  

数组不为空

     

您尝试访问的索引在数组大小范围内。

在您的代码中,在访问args [0]之前添加条件,如下所示:

  if(clargs!=0) int a = Integer.parseInt(args[0]);

尝试在此条件之前声明int a并在此定义以避免进一步的错误。

完整代码:

class VariousCases 
    {
        public static void main(String[] args) 
        {
            final int clargs = args.length; 
            if (clargs==0) 
            {
                System.out.print(" "); 
                System.out.println("Hello"); 
                for (int i = 0; i < 3; i++) 
                {
                    System.out.print(" "); 
                }
                System.out.println("World !!"); 
            }

            if (clargs==1)
            {
                String s = args[0];
                for (int i = 0; i < s.length(); i++) 
                {
                    for (int j = 0; j < i; j++)
                    {
                        System.out.print(" "); 
                    }
                System.out.println(s); 
                }
            }

            if (clargs==2) 
            {
                System.out.println("ARGUMENT 1:" + " " + (args[0])); 
                System.out.println("ARGUMENT 2:" + " " + (args[1])); 
            }

            if (clargs==3) 
            {
                final int a = Integer.parseInt(args[0]);
                final int b = Integer.parseInt(args[1]);
                final int c = Integer.parseInt(args[2]); 

                if (a * b == c) {
                    System.out.println("1*2=3"); 
                }
                else if (a * c == b) {
                    System.out.println("1*3=2");
                }
                else if (b * a == c) {
                    System.out.println("2*1=3");
                }
                else if (b * c == a) {
                    System.out.println("2*3=1");
                }
                else if (c * a == b) {
                    System.out.println("3*1=2"); 
                }
                else if (c * b == a) {
                    System.out.println("3*2=1");
                }
                else 
                {
                    System.out.println("None"); 
                }
                System.out.println(); 
            }

            if (args.length == 4) {
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]); 
                int c = Integer.parseInt(args[2]);
                int d = Integer.parseInt(args[3]);  
                if (a == b && a == c && a == d) {
                    System.out.println("1"); 
                }
                else if (a != b && b != c && c != d) {
                    System.out.println("4");
                }
                else if (a == b) {
                    System.out.println("2");
                }
                else if (c == d && a != b) {
                    System.out.println("3");
                }
                else if (b == c) {
                    System.out.println("2");
                }
                else if (a == b && a == c) {
                    System.out.println("3");
                }
                else if (d == c && d == b) {
                    System.out.println("3");
                }
                else if (a == d) {
                    System.out.println("2");
                }
                else if (a == c && b == d) {
                    System.out.println("2");
                }
                else if (a == d && b != c) {
                    System.out.println("3");
                }
            }

            // Add the condition to check the arguments length is greater and the index you are trying to access is within the range of the array length
            if(clargs!=0)
                int a = Integer.parseInt(args[0]);
            long answer = 1;
            long result = 0; 
            if (a < 0) {
                for(int i = 1; i < args.length; i++) {
                    answer *=  Integer.parseInt(args[i]);
                }
                System.out.println(answer);
            }
            else if (a == 0) {
                for(int i = 0; i < args.length; i++) {
                    result += Integer.parseInt(args[i]);
                }
                System.out.println(result); 
            }
            else if (a == 1) {
                for(int i = 0; i < args.length; i++) {
                    result += Integer.parseInt(args[i]);
                }
                System.out.println(result); 
            }
            else if (a == 2) {
                for(int i = 2; i < args.length; i++) {
                    if (i % 2 != 0){
                    answer += Integer.parseInt(args[i]); 
                    } 

                }
                System.out.println(answer); 
            }
            else if (a == 3) {
                for (int i = 2; i < args.length; i++) {
                    if (i % 3 != 0) {
                    answer += Integer.parseInt(args[i]);
                    }
                }
                System.out.println(answer); 
            } 
    }
    }

答案 3 :(得分:0)

调用第94行而不检查args的大小。在尝试访问阵列单元之前,请尝试检查大小是否正常。

$(".input-group.date input")