String [] args参数:java.lang.ArrayIndexOutOfBoundsException [英] String[] args parameter: java.lang.ArrayIndexOutOfBoundsException

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

问题描述

我是一名Java学习者,正在尝试将该程序添加两个数字.运行此程序时,我收到此错误消息..

Hi i am a java learner and trying to make this program for add two number. While running this i am getting this error msg..

线程主"中的异常java.lang.ArrayIndexOutOfBoundsException:0在addnumber.main(addnumber.java:16)Java结果:1

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at addnumber.main(addnumber.java:16) Java Result: 1

public class addnumber{
   public static void main(String[] args) {
        String x,y;
        int a,b,c;
        x=args[0];
        y=args[1];
        a=Integer.parseInt(x);
        b=Integer.parseInt(y);
        c=a+b;
        System.out.println(c);
   }
}

我知道我可以使用Scanner类或字符串生成器类,但是这段代码有什么问题呢?

I know i can use Scanner class or string builder class however whats wrong with this code?

推荐答案

如果使用args-array,则必须从外部(例如从控制台)为程序提供一些参数.

If you use the args-array you have to give the programm some parameters from outside for example from the console.

因此,打开控制台并转到.java文件所在的目录,并使用手动进行编译

So open the console and go to the directory where the .java file is and compile it manually with

javac Addnumber.java

现在,您应该在那里看到一个.class文件.

Now you should see a .class file there.

比写这样的电话

java Addnumber 5 9 

您的参数将为5和9.

还要用大写字母写类名

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

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