使用 VTD-XML 的 Scala 程序中的 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 库 它编译得很好,但在执行时打印一个异常
:java.lang.ArrayIndexOutOfBoundsException: -1
我已经解决了代码中的 while 问题。但问题是我总是为 ap.evalXPAth
得到 -1
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)
}
}
}
I have imported VTD-XML Library
It compiles Well but On Execution prints an Exception
:java.lang.ArrayIndexOutOfBoundsException: -1
I have solved the while issue in the code. But the problem is I always get -1 for ap.evalXPAth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信答案就
在 In Scala assignment returns Unit (equivalent to void in Java) 行中,因此这个循环无法终止。例如下面的程序无限循环
I believe the answer lies in the line
In Scala assignment returns Unit (equivalent to void in Java) and hence this loop cannot terminate. For example the program as follows loops infinitely
正如约翰·麦克雷所说; while 检查在 scala 中不会执行。相反,您可以为 -1 结尾的调用定义一个新的 while 结构:
这是一个从键盘读取直到您给出 -1 的调用的简化示例:
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:
This is a simplified example of an invocation that read from the keyboard until you give -1: