I am writing a parser program in Scala that should read input using "args" and pars it. It doesn't matter I use:
我正在Scala中编写一个解析器程序,它应该使用“args”读取输入并对其进行解析。我使用没关系:
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:
该程序给我运行时异常的数组索引超出界限!发送2个值作为输入:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
what should I do? I am confused!
我该怎么办?我很困惑!
3 个解决方案
#1
4
Your exception:
你的例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
Is because you are not breaking the while
loop; the args
parameter never change it's size, so your while
will go forever util j
exceed the size of args
.
是因为你没有打破while循环; args参数永远不会改变它的大小,所以你的while将永远使用util j超过args的大小。
Maybe you could try:
也许你可以试试:
int i = 0
while (i < args.length){
// some code here
i++;
}
or
要么
for(int i = 0; i < args.length; i++){
// some code here
}
If you want to iterate through all the array
如果要遍历所有数组
#2
3
From what you describe, you need to iterate through your array while your index is lower than the maximum array size. If you merely compare args.length value, the loop condition will continue to evaluate to a truth value infinitely, since args.length will always be different than 0 (if not changed).
根据您的描述,您需要在索引低于最大数组大小时迭代数组。如果仅比较args.length值,则循环条件将继续无限地评估为真值,因为args.length将始终不同于0(如果未更改)。
You need something along the lines of:
你需要的东西是:
for(i <- 0 until array.length){
...
You can find extra information on accessing and iterating over arrays here and here
您可以在此处和此处找到有关访问和迭代数组的额外信息
#3
1
Consider iterating over args
without using indexed references (the source of out-of-bounds error),
考虑在不使用索引引用(越界错误的来源)的情况下迭代args,
for ( arg <- args ) yield {
if (Files.exists(Paths.get(arg))) xmlFile
else ""
}.mkString(" ")
This for comprehension yields a collection of String
which is converted to a space-separated string with mkString
.
这用于理解产生String的集合,该集合被转换为具有mkString的空格分隔的字符串。
#1
4
Your exception:
你的例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
Is because you are not breaking the while
loop; the args
parameter never change it's size, so your while
will go forever util j
exceed the size of args
.
是因为你没有打破while循环; args参数永远不会改变它的大小,所以你的while将永远使用util j超过args的大小。
Maybe you could try:
也许你可以试试:
int i = 0
while (i < args.length){
// some code here
i++;
}
or
要么
for(int i = 0; i < args.length; i++){
// some code here
}
If you want to iterate through all the array
如果要遍历所有数组
#2
3
From what you describe, you need to iterate through your array while your index is lower than the maximum array size. If you merely compare args.length value, the loop condition will continue to evaluate to a truth value infinitely, since args.length will always be different than 0 (if not changed).
根据您的描述,您需要在索引低于最大数组大小时迭代数组。如果仅比较args.length值,则循环条件将继续无限地评估为真值,因为args.length将始终不同于0(如果未更改)。
You need something along the lines of:
你需要的东西是:
for(i <- 0 until array.length){
...
You can find extra information on accessing and iterating over arrays here and here
您可以在此处和此处找到有关访问和迭代数组的额外信息
#3
1
Consider iterating over args
without using indexed references (the source of out-of-bounds error),
考虑在不使用索引引用(越界错误的来源)的情况下迭代args,
for ( arg <- args ) yield {
if (Files.exists(Paths.get(arg))) xmlFile
else ""
}.mkString(" ")
This for comprehension yields a collection of String
which is converted to a space-separated string with mkString
.
这用于理解产生String的集合,该集合被转换为具有mkString的空格分隔的字符串。