ArrayIndexOutOfBoundsException异常,

时间:2014-09-23 15:22:46

标签: java arrays command-line-arguments

您好我正在尝试通过命令行参数提供输入来获取二进制操作,并将异常作为"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");
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

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

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

此行未提供长度为3或4的strArray

根据提到的错误消息

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

此行已抛出异常,表示splits [1]未退出,因此拆分[2]并拆分[3]

答案 1 :(得分:0)

你的问题是

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