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

AnsigT
  • 91
  • 6
  • Runtime exceptions are usually debugged using stacktraces... Could you be more specific about your issue? – Palimondo Feb 26 '11 at 12:24
  • The way you use try/catch here is bad: you catch ALL problems and print single message out of context. BTW your problem is most probably unrelated to scala. – Palimondo Feb 26 '11 at 12:34

2 Answers2

4

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) }
John McCrae
  • 487
  • 2
  • 5
  • 12
  • 1
    @vtd-xml-author Not doing that would be a work-around. There's really no need to assign and test at the same time, and plenty ways of avoiding it. But if one insists, one could always write `while({i = ap.evalXPath; i != -1}) {`. – Daniel C. Sobral Feb 27 '11 at 02:04
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:

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) 
}
thoredge
  • 12,237
  • 1
  • 40
  • 55