java.lang.ArrayIndexOutOfBoundsException:0 [英] java.lang.ArrayIndexOutOfBoundsException: 0

查看:349
本文介绍了java.lang.ArrayIndexOutOfBoundsException:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一本书学习java。有这样的锻炼,我不能得到正常工作。它增加了使用的Java类双两个双打。当我尝试在Eclipse中运行这个code它给我的错误的称号。

 公共静态无效的主要(字串[] args){    双D1 = Double.valueOf(参数[0]);
    双D2 = Double.valueOf(参数[1]);
    双重结果= d1.doubleValue()+ d2.doubleValue();
    的System.out.println(参数[0] +++ ARGS [1] +=+结果);}


解决方案

问题

ArrayIndexOutOfBoundsException异常:0 表示,该指数 0 不是你的阵列<$ C $有效的索引C> ARGS [] ,这反过来又意味着你的数组是空的。

的main()法的这种特殊情况下,这意味着无参数传递到您的命令行程序。

可能的解决方案


  • 如果你在命令行运行程序,不要忘记通过2个参数的命令。


  • 如果你在Eclipse中运行程序,则应该在运行配置命令行参数。转到运行&GT;运行配置... ,然后选择参数设置页为您运行配置和添加一些参数的程序参数的区域。


注意你应该在你的主要方法开始处理情况,没有足够的参数时,的情况下,像这样的东西:

 如果(args.length 2){
    通信System.err.println(没有得到足够的论点。);
    返回;
}

这将正常失败使你的程序崩溃,而不是

I am learning java using a book. There is this exercise that I can't get to work properly. It adds two doubles using the java class Double. When I try to run this code in Eclipse it gives me the error in the title.

public static void main(String[] args) {

    Double d1 = Double.valueOf(args[0]);
    Double d2 = Double.valueOf(args[1]);
    double result = d1.doubleValue() + d2.doubleValue();
    System.out.println(args[0] + "+" + args[1] + "=" + result);

}

解决方案

Problem

This ArrayIndexOutOfBoundsException: 0 means that the index 0 is not a valid index for your array args[], which in turn means that your array is empty.

In this particular case of a main() method, it means that no argument was passed on to your program on the command line.

Possible solutions

  • If you're running your program from the command line, don't forget to pass 2 arguments in the command.

  • If you're running your program in Eclipse, you should set the command line arguments in the run configuration. Go to Run > Run configurations... and then choose the Arguments tab for your run configuration and add some arguments in the program arguments area.

Note that you should handle the case where not enough arguments are given, with something like this at the beginning of your main method:

if (args.length < 2) {
    System.err.println("Not enough arguments received.");
    return;
}

This would fail gracefully instead of making your program crash.

这篇关于java.lang.ArrayIndexOutOfBoundsException:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆