ArrayIndexOutofBoundException In Scala Program Usind VTD-XML

倖福魔咒の 提交于 2019-12-08 19:33:29

I believe the answer lies in the line

while((i=ap.evalXPath)!= -1) {

In Scala assignment returns Unit (equivalent to void in Java) and hence this loop cannot terminate. For example the program as follows loops infinitely

scala> var i = 0
i: Int = 0

scala> while((i = i + 1) != 10) { println(i) }
<console>:7: warning: comparing values of types Unit and Int using `!=' will always yield true
       while((i = i + 1) != 10) { println(i) }

As John McCrae said; the while check will not fly in scala. Instead you can define a new while construction like this for -1-ending calls:

def whileNotEmpty(whileF: () => Int)(doF: (Int) => Unit): Unit = { 
  val n = whileF()
  if (n != -1) { 
    doF(n)
    whileNotEmpty(whileF)(doF) 
  } 
}

This is a simplified example of an invocation that read from the keyboard until you give -1:

whileNotEmpty(() => Console.readLine.toInt) { 
  n => println(n) 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!