2012-04-04 17 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); 

申し訳ありませんが、私はそれが正しいことを伝えることができます。このコードのポイントは、先頭の最初の文字列を読み込み、配列に "and"、 "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

に達したことを意味し

+1

このエラーは、プログラムが配列の負の最初のインデックス、つまり、myArray [-1]配列内の最小のインデックスです。 –

答えて

6

また、あなたが必要とするすべての行番号に従っていることに注意してください。たとえば、最初に探すのは '45'です。

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

最初に書き込んだ方法に対処してください。

+0

ええと、存在しない行もあるので、数字は何の意味もないと思った。しかし、私は見て、それは否定できないはずです。少なくとも、メソッド内の最初のチェックの全体点は – Andy

+0

です。私は助けに感謝します。 – Andy

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は常にfalseとなります。これは、temp.lengthが0であっても、else句が常に実行されることを意味します。

+0

ありがとう!私もそれに気づいた。しかし、私のコードは少なくともスカラーのために悪いコードでいっぱいです。エラーを1つずつ修正する必要があります。 – Andy

+0

あなたが言ったことを踏み出したにもかかわらず、私はelseパートでエラーが発生しています。私の次の例は "(とx(とy z))"になるだろう。それが実行されると、zの問題が発生するので、yが渡されたと仮定します。あなたは、なぜ私にそのエラーを与えるのかを見ることができると思いますか? – Andy

+0

@Andy: 'java.lang.NumberFormatExceptionが発生しました:入力文字列:" z "の場合 ... Driver $ .stringParse $ 1(Temp.scala:47)'行47では、最後のものをあなたの 'ArrayBuffer [String]'スタックをインデックスに入れましたが、最後にスタックにプッシュしたものは "z"でした(49行目 - 'temp + = exp(b).toString')。 – rampion

関連する問題