2014-09-23 80 views
-1

您好我想获得的二元运算,通过给通过命令行参数的输入,而我得到的异常为"Exception in thread "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at assignment.CommandLineargs.main(CommandLineargs.java:7)"ArrayIndexOutOfBoundsException异常,

这里是我的代码:

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

你能加注释的行号您的问题是什么? – ControlAltDel 2014-09-23 15:24:59

+0

您可以提供整个堆栈跟踪以及您在执行提示时发出的命令是什么... – StackFlowed 2014-09-23 15:25:06

+1

为什么要将数组转换为字符串,然后再将其分割为数组?还要注意数组是基于0的。所以,如果你期待3个元素,然后使用索引0,1,2. – 2014-09-23 15:28:12

回答

0

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

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

这一行是不给长度3或4

按照错误消息的strArray提到

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

此线已经抛出异常,表明分裂[1]不退出,因此分裂[2]和分裂[3]

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 
+0

谢谢所有,我现在已更新代码,位置已更改,但我仍然得到“线程中的异常”主“java.lang.ArrayIndexOutOfBoundsException:0 (请参阅参考资料中的“\t”)。参考:Command.CommandLineargs.main(CommandLineargs.java:7) “请指导 – 2014-09-23 15:36:51

+0

更新代码:public class CommandLineargs {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;默认:System.out.println(“Invalid Operator selected”);}}} – 2014-09-23 15:37:11

+0

你需要看到我的答案。前3行会给你为什么你有错误... – StackFlowed 2014-09-23 15:37:15

相关问题