2012-04-04 85 views
0

我收到了這個錯誤,但它並不是非常有用,因爲它並沒有將我指向發生錯誤的地方。錯誤之後的數字意味着什麼?我一直在查看過去幾個小時的錯誤,但我並沒有真正知道錯誤來自哪裏。我的代碼是:Scala中的「java.lang.ArrayIndexOutOfBoundsException:-1」中的數字意味着什麼?

val str = "(and x y)"; 

def stringParse (exp: String, expreshHolder: ArrayBuffer[String]): ArrayBuffer[String] = { //takes three arguments, string, int, arraybuffer 

    var b = 0; //position of where in the expression String I am currently in 
    var temp = expreshHolder; //holder of expressions without parens 
    if(temp == 0) b = 0; else {b = temp(temp.length-1).toInt; temp.remove(temp.length-1)} //this sets the position of wherever the string was read last plus removes that check from the end of the ArrayBuffer 
    var arrayCounter = temp.length; //just counts to make sure an empty spot in the array is there to put in the strings 

    if(exp(b) == '(') { 
     b = b + 1; 

     while(exp(b) == ' '){b = b + 1;} //point of this is to just skip any spaces between paren and start of expression type 
     if(exp(b) == 'a') { 
      //first create the 'and', 'or', 'not' expression types to figure out 
      temp += exp(b).toString; 
      b = b+1; 
      temp(arrayCounter) = temp(arrayCounter) + exp(b).toString; //concatenates the second letter 
      b = b+1; 
      temp(arrayCounter) = temp(arrayCounter) + exp(b).toString; //concatenates the last letter for the expression type 
      arrayCounter+=1 
      //this part now takes the symbols and puts them in an array 
      b+=1; 
      while(exp(b) == ' ') {b+=1;} //just skips any spaces until it reaches the first symbol 
      if(exp(b) == '(') { temp += b.toString; temp = stringParse(exp, temp); 
      b = temp(temp.length-1).toInt; 
      temp.remove(temp.length-1); 
      arrayCounter = temp.length 
      } else { 
      temp += exp(b).toString; 
      arrayCounter+=1; b+=1; } 
      while(exp(b) == ' ') {b+=1;} //just skips any spaces until it reaches the second symbol 

      if(exp(b) == '(') { temp += b.toString; temp = stringParse(exp, temp); 
          b = temp(temp.length-1).toInt; 
          temp.remove(temp.length-1); 
          arrayCounter = temp.length } else {temp += exp(b).toString; arrayCounter+=1; b+=1; }  
     } 
    temp; 
    } else { 
     temp(arrayCounter) +="failed"; temp;} //this is just incase it fails and I should probably check this incase it fails when it doesnt encounter an opening paren 
}//end of while loop 

hold = stringParse(str, ho); 
for(test <- hold) println(test); 

對不起,代碼量不大,但從我能告訴它是正確的。這段代碼的重點在於讀取頂部的第一個字符串,並將「和」,「x」,「y」放在數組中。它假設在更復雜的版本上完成它,但我首先嚐試使用簡單版本進行測試,並確保它可以正常工作。我得到的錯誤是:

java.lang.ArrayIndexOutOfBoundsException: -1 
at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:45) 
at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:44) 
at Driver$.stringParse$1(Driver.scala:19) 
at Driver$.main(Driver.scala:60) 
at Driver.main(Driver.scala) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78) 
at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24) 
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88) 
at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78) 
at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101) 
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33) 
at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40) 
at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56) 
at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80) 
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89) 
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala) 

任何幫助能夠讀取此錯誤消息,並瞭解它發生的地方?提前致謝。

+1

錯誤表示您的程序嘗試去數組中的負第一個索引,即'myArray [-1]',這不可能爲0是數組中最低的索引。 – 2012-04-04 23:48:35

回答

6

這意味着你正在遞減變量已達到-1

另外請注意,所有你需要做的就是按照行號。例如,首先看的是'45'行

scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:45) 

解決您先寫的方法。

+0

嗯,我認爲數字意味着什麼,因爲也有行不存在。但是我看了,它不應該達到負面。至少這是方法 – Andy 2012-04-05 00:08:05

+0

第一次檢查的全部內容,我非常感謝幫助。 – Andy 2012-04-05 00:54:55

6

的錯誤是對Driver.scala行19:

at Driver$.stringParse$1(Driver.scala:19) 

採取甘德:

if(temp == 0) b = 0; else {b = temp(temp.length-1).toInt; temp.remove(temp.length-1)} 

tempArrayBuffer[String],不是一個數字,所以temp == 0永遠是假的。這意味着else子句將始終執行,即使temp.length爲0時也是如此。

+0

謝謝!我也注意到了它。但是我的代碼至少有很多糟糕的代碼。一定要修復錯誤。 – Andy 2012-04-05 01:11:21

+0

儘管你說了什麼,但我得到了其他部分的錯誤。我的下一個例子是「(和x(和y z))」。當它運行時,它遇到了z的問題,所以我認爲y已經過去了。你想也許你可以看看爲什麼它給了我這個錯誤? – Andy 2012-04-05 01:27:15

+0

@Andy:你得到'java.lang.NumberFormatException:對於輸入字符串:「z」 ...在Driver $ .stringParse $ 1(Temp.scala:47)'因爲在第47行,你嘗試轉換最後一件事你的'ArrayBuffer [String]'堆棧到一個索引中,但是你最後推入堆棧的是「z」(在第49行 - 「temp + = exp(b).toString」)。 – rampion 2012-04-05 18:21:01