How can I overcome ArrayIndexOutOfBoundException for Integer.parseInt(args[0])?

杀马特。学长 韩版系。学妹 提交于 2020-07-04 02:47:01

问题


I've seen below code in one of the video tutorial.There its executes fine but while I'm trying to execute in my system, it is compiling fine but I'm getting runtime error saying,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

class Test13 
{
    public static void main(String[] args) 
    {   
        int i = Integer.parseInt(args[0]);
        System.out.println(i);
    }
}

Can someone please guide me what's wrong with this code and how to rectify?

Thanks in advance!


回答1:


ArrayIndexOutOfBoundsException occurs when you try to access an element at an index which does not exist in the array.

For example: suppose int a[]={2,4,5,10,3} is an array.

the size of array is 5 and index starts from 0.

Which means your array ranges from index 0 to index 4 where element at index 0 is the first element i.e. 2 and element at index 4 is the last element i.e. 3

if you try to access any elements at an index which is not in the range 0 to 4 it will show you ArrayIndexOutOfBoundsException because no such index exists in the array.

Now in your case args is command line argument which means you have to pass parameters when you run your code.

If you are running your code from terminal then after java yourclassname you have to pass parameters.

For example: java yourclassname 10 20 30

here 10 20 30 are your command line arguments which get stored in your args array and args[0]=10 args[1]=20 args[2]=30

If you haven't passed any arguments during running your code your args is empty and therefore you will get ArrayIndexOutOfBoundsException

hope it helps you to understand the concept of command line arguments.




回答2:


It looks you're not passing in any parameters when you run your code.

From the Java doc for ArrayIndexOutOfBoundsException: 'Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.'

Therefore you're trying to pull a value from the args array with an index 0, but the array is less than this size (in this case it's empty).

In order for this to prevent an exception being thrown you can apply a size check around the statement.

if(args.length>0) {
    int i = Integer.parseInt(args[0]);
    System.out.println(i);
}



回答3:


if (args.length > 0) {
   Integer.parseInt(args[0])
}



回答4:


You should pass an argument to your main method when executing your application. For example, in command line type:

java Test13 123

Then the output should be: 123

Moreover you can check argument availability by check args.length, like what Adam said in prev answer.




回答5:


You are not passing any argument while running your program. If you are running from command line then write java Test13 1. you will be able to get your result in output. If you are using eclipse to run your program then provide your arguments in "Run Configuration" ->Arguments tab -> Program Arguments text box -> give value as 1.

You will be able to run your program




回答6:


This is a runtime error as you might have observed the compilation goes smoothly. Now, why this happens is because you have to pass the "argument" while you give the run command (as in 2)

1. You might have run this command, without actually passing the argument [0]:

When you do this, the array is of length 0 as there are no arguments passed to the main function. Which is how the program is expected to run according to your code.

$ java Test13

This will give an error output. The one you got, with Array exception.

2. Try running this command, that is, type a number along with your command:

$ java Test13 727

Here, you are passing the argument [0] as 727 to the main function. Which adds the element to your array. This should work fine.

Similarly, suppose you have more arguments like [0], [1] and [2] And, you forget to add numbers after '727' like in command 2. You must get a similar error. Unless you do this (giving three inputs for your command):

$ java Test13 727 837 9

This means you need to give your input during the run command itself. However, in C++ with 'cin' in your code, you can give input after running the first command. Java is safer this way, although no real threat exists here.

Hope this works for you :)



来源:https://stackoverflow.com/questions/34720857/how-can-i-overcome-arrayindexoutofboundexception-for-integer-parseintargs0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!