2014-09-23 136 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

相關問題