2013-10-26 27 views
0

我正在構建一個程序,充當帶有內存的計算器,因此您可以給變量及其值。每當我試圖重新定義一個變量時,a = 5,到a = 6,我都會得到一個索引超出界限的錯誤。當我嘗試重新定義一個變量時,我得到一個索引超出界限錯誤

public static void main(String args[]) 
{ 
    LinkedHashMap<String,Integer> map = new LinkedHashMap<String,Integer>(); 
    Scanner scan = new Scanner(System.in); 
    ArrayList<Integer> values = new ArrayList<>(); 
    ArrayList<String> variables = new ArrayList<>(); 

    while(scan.hasNextLine()) 
    { 
     String line = scan.nextLine(); 
     String[] tokens = line.split(" "); 


     if(!Character.isDigit(tokens[0].charAt(0)) && !line.equals("clear") && !line.equals("var")) 
     { 
      int value = 0; 
      for(int i=0; i<tokens.length; i++) 
      { 
       if(tokens.length==3) 
       { 
        value = Integer.parseInt(tokens[2]); 
        System.out.printf("%5d\n",value); 
        if(map.containsKey(tokens[0])) 
        { 
         values.set(values.indexOf(tokens[0]), value); 
         variables.set(variables.indexOf(tokens[0]), tokens[0]); 
        } 
        else 
        { 
         values.add(value); 
        } 
        break; 
       } 

       else if(tokens[i].charAt(0) == '+') 
       { 
        value = addition(tokens, value); 
        System.out.printf("%5d\n",value); 
        variables.add(tokens[0]); 
        if(map.containsKey(tokens[0])) 
        { 
         values.set(values.indexOf(tokens[0]), value); 
         variables.set(variables.indexOf(tokens[0]), tokens[0]); 
        } 
        else 
        { 
         values.add(value); 
        } 
        break; 
       } 

       else if(i==tokens.length-1 && tokens.length != 3) 
       { 
        System.out.println("No operation"); 
        break; 
       } 
      } 
      map.put(tokens[0], value); 

     }  

     if(Character.isDigit(tokens[0].charAt(0))) 
     { 
      int value = 0; 
      if(tokens.length==1) 
      { 
       System.out.printf("%5s\n", tokens[0]); 
      } 

      else 
      { 
       value = addition(tokens, value); 
       System.out.printf("%5d\n", value); 
      } 
     } 

     if(line.equals("clear")) 
     { 
      clear(map); 
     } 

     if(line.equals("var")) 
     { 
      variableList(variables, values); 
     }  
    } 
} 

public static int addition(String[] a, int b) 
{ 
    for(String item : a) 
    { 
     if(Character.isDigit(item.charAt(0))) 
     { 
      int add = Integer.parseInt(item); 
      b = b + add; 
     } 
    } 
    return b; 
} 

public static void clear(LinkedHashMap<String,Integer> b) 
{ 
    b.clear(); 
} 

public static void variableList(ArrayList<String> a, ArrayList<Integer> b) 
{ 
    for(int i=0; i<a.size(); i++) 
    { 
     System.out.printf("%5s: %d\n", a.get(i), b.get(i)); 
    } 
} 

我包含了整個代碼,因爲我不知道錯誤是從哪裏來的。

Error

我假設的錯誤是從的ArrayList存儲的值出現。

回答

3

這條線:

values.set(values.indexOf(tokens[0]), value); 

拋出ArrayIndexOutOfBoundsException

您正在查找values,其中您的變量名稱"a"當前存在,顯然要覆蓋它。

但是,這沒有意義:valuesListInteger,因此它永遠不會包含變量名稱"a"。因此,List.indexOf(Object)返回-1。當你試圖調用List.set(E, int)通過-1它會炸燬。

+0

此外,你的'for'循環看起來沒有必要? –

+0

我說的是錯的,但是你的問題已經解決了。你在說什麼循環? – user2770254

+1

啊,可能'不必要'不對,但有點令人困惑:-)你有這樣一行:'for(int i = 0; i

相關問題