I am writing a parser program in Scala that should read input using "args" and pars it. It doesn't matter I use:
while(!args.isEmpty){
if (Files.exists(Paths.get(args(j)))){
Statement=Statement.concat(inputXml)
Statement=Statement.concat(" ")
println(j)
}
else{
Statement=Statement.concat(args(j))
Statement=Statement.concat(" ")
println(j)
}
j=j+1
}
or
while(args.length !=0) {
if (Files.exists(Paths.get(args(j)))){
Statement=Statement.concat(inputXml)
Statement=Statement.concat(" ")
println(j)
}
else{
Statement=Statement.concat(args(j))
Statement=Statement.concat(" ")
println(j)
}
j=j+1
}
The program gives me run time exception of array index out of bound! sending 2 values as input:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
what should I do? I am confused!
Your exception:
Is because you are not breaking the
while
loop; theargs
parameter never change it's size, so yourwhile
will go forever utilj
exceed the size ofargs
.Maybe you could try:
or
If you want to iterate through all the array