是什么导致了这个 ArrayIndexOutOfBoundsException?
我是斯卡拉新手。
这是简单的代码
object SimpleIfStatementDemo {
def main(args: Array[String]) {
var numA: Int = args(0).toInt
var numB: Int = args(1).toInt
if(numA>numB) {
print(numA)
} else {
print(numB)
}
}
}
,可以编译,但不能运行。这是错误,我明白了
java.lang.ArrayIndexOutOfBoundsException: 0 at
Main$.main(SimpleIfSamentDemo.scala:5) 位于 Main.main(SimpleIfSamentDemo.scala) 位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 处 scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78) 在 scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24) 在 scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88) 在 scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78) 在 scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101) 在 scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33) 处 scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40) 在 scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171) 在 scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188) 在 scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188) 在 scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157) 在 scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131) 在 scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131) 在 scala.tools.nsc.util.package$.waitingForThreads(package.scala:26) 在 scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130) 在 scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188) 处 scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201) 在 scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58) 在 scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80) 在 scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89) 在 scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
谢谢
I am new to scala.
here is simple code
object SimpleIfStatementDemo {
def main(args: Array[String]) {
var numA: Int = args(0).toInt
var numB: Int = args(1).toInt
if(numA>numB) {
print(numA)
} else {
print(numB)
}
}
}
it's can be compile,but can not be run it. here is error i got it
java.lang.ArrayIndexOutOfBoundsException: 0 at
Main$.main(SimpleIfSatementDemo.scala:5) at
Main.main(SimpleIfSatementDemo.scala) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) at
scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
at
scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
at
scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
at
scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
at
scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33) at
scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40) at
scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
at
scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at
scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
at
scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
at
scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at
scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
at
scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188) at
scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
at
scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)
at
scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
at
scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该程序未传递足够的参数(它至少需要两个;请参阅 java 命令手册了解如何指定这些)。考虑将这段代码作为 main 方法的第一行:
然后在 Scala REPL 上尝试一下,看到类似的爆炸:
如何使用这些新知识使程序更加健壮(并且不抛出异常)?
快乐编码。
迈向 Scala 一文提供了传递和使用参数的示例。
The program is not being passed enough arguments (it requires at least two; see the java command manual for how these can be specified). Consider putting this code as the first line of the main method:
And then try this on the Scala REPL to see a similar explosion:
How can this new knowledge be used to make the program more robust (and not throw the exception)?
Happy coding.
The article First Steps to Scala has examples of passing and using arguments.
假设您在命令行上运行 2.9.0 或更高版本,并且您的源代码包含在名为
SimpleIfStatementDemo.scala
的文件中:将为您的程序提供参数 1 和 2。
在 2.9.x 和 2.8.x 上,您还可以将源代码编译为
class
文件,然后编译为 JVM 的中间字节代码。一个好的做法是将它们放在子目录下(本例中为classes
):如果您在 IDE 中运行,则必须弄清楚如何编辑 run 配置以向运行命令添加参数。
Assuming you run at the command line with 2.9.0 or later and that your source code is contained in a file called
SimpleIfStatementDemo.scala
:will provide the arguments 1 and 2 to your program.
On 2.9.x and 2.8.x, you can also compile the source into
class
files compiled into an intermediate byte code for the JVM. A good practice is to have them under a subdirectory (classes
in this example):If you run within an IDE, you have to figure out how to edit the run configuration to add arguments to the run command.