【问题标题】:ArrayIndexOutOfBoundsException, [duplicate]ArrayIndexOutOfBoundsException,[重复]
【发布时间】:2014-11-17 21:12:42
【问题描述】:

您好,我正在尝试通过命令行参数提供输入来获取二进制操作,我得到的异常为"Exception in thread "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at assignment.CommandLineargs.main(CommandLineargs.java:7)"

这是我的代码:

import java.util.Arrays;
public class CommandLineargs {


        public static void main(String[] args) {
            int operand1 = Integer.parseInt(args[0]);
            int operand2 = Integer.parseInt(args[1]);
            char binary_operator = args[2].charAt(0);
            System.out.print(args[0] + args[2] + args[1] + " = ");
            switch(binary_operator) {
                case ('+'):
                    System.out.println(operand1 + operand2); break;
                case ('-'):
                    System.out.println(operand1 - operand2); break;
                case ('*'):
                    System.out.println(operand1 * operand2); break;
                case ('/'):
                    System.out.println(operand1 / operand2); break;
                default:
                    System.out.println("Invalid Operator selected");
            }
        }
    }

【问题讨论】:

  • 您能否添加注释,指出您的问题所在的行号?
  • 您能否给出整个堆栈跟踪以及您从提示发出执行的命令是什么...
  • 为什么要将数组转换为字符串,然后再将其拆分为数组?另请注意,数组是基于 0 的。因此,如果您期望 3 个元素,则使用索引 0、1、2。

标签: java arrays command-line-arguments


【解决方案1】:

你的问题是

String[] splits = strArray.split("");

你用什么分割它???

我写了一个类似的程序:

public static void main (String[]args) {
    String str = "((1+2)*(3+4))-5";
    if(isValid(str)){
        expandString(str);
    }
}

public static boolean isValid(String s) {
    int totalParenthesis = 0;
    for (int i = 0; i < s.length(); i++) {
        if (s.charAt(i) == '(') {
            totalParenthesis++;
        } else if (s.charAt(i) == ')') {
            totalParenthesis--;
        }
        if (totalParenthesis < 0) {
            return false;
        }
    }
    if (totalParenthesis != 0) {
        return false;
    }
    return true;
}

private static void expandString(String str) {
    System.out.println("Called with : "+str);
    if(!(str.contains("("))){
        evalueMyExpresstion(str);
        return;
    }
    String copyString=str;
    int count=-1,positionOfOpen=0,positionOfClose=0;
    for(Character character : str.toCharArray()) {
        count++;
        if(count==str.toCharArray().length){
            evalueMyExpresstion(str);
            return;
        } else if(character.equals('(')) {
            positionOfOpen=count+1;
        } else if(character.equals(')')) {
            positionOfClose=count;
            copyString = str.substring(0, positionOfOpen - 1) + evalueMyExpresstion(
                        str.substring(positionOfOpen, positionOfClose)) + str.substring(positionOfClose + 1);
            System.out.println("Call again with : "+copyString);
            expandString(copyString);
            return;
        }
    }
}

private static String evalueMyExpresstion(String str) {
    System.out.println("operation : "+str);
    String[] operation;
    int returnVal =0;
    if(str.contains("+")){
        operation = str.split("\\+");
        returnVal=Integer.parseInt(operation[0])+ Integer.parseInt(operation[1]);
        System.out.println("+ val : "+returnVal);
        return Integer.toString(returnVal);
    } else if (str.contains("*")){
        operation = str.split("\\*");
        returnVal=Integer.parseInt(operation[0])* Integer.parseInt(operation[1]);
        System.out.println("* val : "+returnVal);
        return Integer.toString(returnVal);
    } else if (str.contains("-")){
        operation = str.split("\\-");
        returnVal=Integer.parseInt(operation[0])- Integer.parseInt(operation[1]);
        System.out.println("- val : "+returnVal);
        return Integer.toString(returnVal);
    }
    System.out.println(str);
    return Integer.toString(returnVal);
}

输出看起来像:

Called with : ((1+2)*(3+4))-5
operation : 1+2
+ val : 3
Call again with : (3*(3+4))-5
Called with : (3*(3+4))-5
operation : 3+4
+ val : 7
Call again with : (3*7)-5
Called with : (3*7)-5
operation : 3*7
* val : 21
Call again with : 21-5
Called with : 21-5
operation : 21-5
- val : 16

【讨论】:

  • 谢谢大家,我现在更新了代码,位置改变了,但我仍然得到“线程中的异常”java.lang.ArrayIndexOutOfBoundsException:0 at assignment.CommandLineargs.main(CommandLineargs.java: 7) "请指导
  • 更新代码:public class CommandLineargs { public static void main(String[] args)int operand1 = Integer.parseInt(args[0]);int operand2 = Integer.parseInt(args[1]) ;char binary_operator = args[2].charAt(0);System.out.print(args[0] + args[2] + args[1] + " = ");switch(binary_operator) { case ('+' ): System.out.println(操作数 1 + 操作数 2); break;case ('-'): System.out.println(operand1 -operand2); break;case ('*'): System.out.println(operand1 *operand2); break;case('/'):System.out.println(operand1/operand2); break;default:System.out.println("选择了无效的运算符");}}}
  • 你需要看看我的回答。前 3 行会告诉你为什么你有错误...
  • 你需要看到 System.out.println(splits.length);就在你拆分字符串之后。该值不是 4 ...
  • 谢谢,应该怎么做才能纠正它?
【解决方案2】:

显然您的数组没有足够的元素。

strArray = strArray.replace("[", "").replace("]", "").replaceAll("[, ]", "");

此行没有给出 长度为 3 或 4

strArray

根据上面提到的错误信息

int operand1 = Integer.parseInt(splits[1]);

这一行一直在抛出异常,表明 splits[1] 没有退出,因此 splits[2] 和 splits[3]

【讨论】:

    猜你喜欢
    • 2015-11-08
    • 2015-09-21
    • 2016-08-31
    • 2011-02-25
    • 1970-01-01
    • 2020-02-07
    • 2012-03-26
    • 2014-08-31
    • 1970-01-01
    相关资源
    最近更新 更多