开发者

What causes this ArrayIndexOutOfBoundsException?

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.NativeMethodAc开发者_StackOverflow社区cessorImpl.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


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:

println("I have " + args.length + " argument(s)")

And then try this on the Scala REPL to see a similar explosion:

val a = Array("Hello world!") // array of one element
a(0)      // access first element, okay
a(1)      // access second element -- KABOOM!

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.


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:

$ scala SimpleIfStatementDemo.scala 1 2

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):

$ scalac -d classes SimpleIfStatementDemo.scala
$ scala -cp classes SimpleIfStatementDemo 1 2

If you run within an IDE, you have to figure out how to edit the run configuration to add arguments to the run command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜