本文介绍了Scala程序Usind VTD-XML中的ArrayIndexOutofBoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.lang._
import com.ximpleware._
object Sample {
def main(args :Array[String])= {
// println("helloo")
try{
var i :Int = -1
val vgen :VTDGen= new VTDGen()
val ap :AutoPilot =new AutoPilot()
ap.selectXPath("CATALOG/CD/COUNTRY/text()")
if(vgen.parseFile("../catalog.xml", false)) {
val vnav :VTDNav = vgen.getNav()
ap.bind(vnav)
while((i=ap.evalXPath)!= -1) {
println(vnav.toString(i))
println(vnav.toNormalizedString(vnav.getText()))
}
ap.resetXPath()
}
}
catch {
case e :Exception => println(e)
}
}
}
我已经导入了VTD-XML库编译良好,但在执行时会显示异常
I have imported VTD-XML LibraryIt compiles Well but On Execution prints an Exception
:java.lang.ArrayIndexOutOfBoundsException: -1
我已经解决了代码中的while问题.但是问题是我总是ap.evalXPAth
I have solved the while issue in the code. But the problem is I always get -1 for ap.evalXPAth
推荐答案
我相信答案就在这行
while((i=ap.evalXPath)!= -1) {
在Scala赋值中返回Unit(在Java中等同于void),因此此循环无法终止.例如下面的程序无限循环
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) }
这篇关于Scala程序Usind VTD-XML中的ArrayIndexOutofBoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!