-2

When I run the code give below, the following message showed up. What does it mean and how do I overcome it in this instance?

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at ifDemo.main(ifDemo.java:5)

public class ifDemo {
public static void main (String [] args)

{
    int x= Integer.parseInt(args[0]);
    double half=0.0;
    if (x!=0)
    {
        half=x/2.0;
        System.out.println(x+ "/2 = "+half);
    }
    if (x==0)
    {
        System.out.println("The value of x is "+x);
    }
    int y=x*5;
    char grade='F';
    if(y>=85)
    {
        grade='A';
    }
    if (y>=70 && y<85)
        grade='C';
    System.out.println("y=  "+y+ "and grade equal to  "+grade);

    }
}
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
josh
  • 121
  • 3
  • 9

8 Answers8

3

Essentially this means that you are trying to access an index in the args array that doesn't exist.

In your code you have:

int x= Integer.parseInt(args[0]);

And this error is complaining that you actually don't have an index 0 in your args array, i.e the array is empty.

To remedy this, you need to pass your program command line arguments when it is run, in other words, run it as java ifDemo Some Integer Here

For further reading try this, also Google is Your Friend

Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
2

While running the code you may not be passing any value that x can get a value from arg[0].

Pass value while running the code, this will solve the issue.

Are you using the command-line for executing it?

Maximin
  • 1,655
  • 1
  • 14
  • 32
  • I am running it through Eclipse IDE. – josh Apr 21 '13 at 07:03
  • Look at this [link](http://stackoverflow.com/questions/7574543/how-to-pass-console-arguments-to-application-in-eclipse). It will be helpful for you. – Maximin Apr 21 '13 at 07:10
2

When you are running the program from the command line you are forgetting to add an argument as so:

java ifDemo.class 1

So essentially the variable args contains absolutely nothing. Therefore, when you try to get the value of args[0] it is throwing the ArrayIndexOutOfBoundsException. You can read more about it here.

What you could add is a form of validation that checks if the user has entered a number and, if they have not, tell them. The code for that would look something like this:

if(args == null || args.length == 0) {
    System.out.println("Please enter a number as a command line argument.");
    System.exit(0);
}

Or you could look into getting input from the console and using a do while loop for validation of that. If you want a code example just let me know!

Kezz
  • 1,680
  • 3
  • 19
  • 37
2

If you are trying to run the program in Command Prompt then be sure to provide the command line argument. I tried running the program and got no errors.

screenshot

Prem Bharath
  • 21
  • 1
  • 2
1
int x= Integer.parseInt(args[0]);

In that line above, you are indexing into an array, assuming it has at least 1 element. Are you sure it has one element in it? Are you calling your java program and passing in a number?

Alan
  • 45,915
  • 17
  • 113
  • 134
1

int x= Integer.parseInt(args[0]);

This is trying to read the first command line argument. It will fail with the exception you describe when there are none.

You should update the program with a check for the command line arguments and add an error message if they are amiss.

Thilo
  • 257,207
  • 101
  • 511
  • 656
1

Run it as java ifDemo xxx where xxx is some integer.

args is an array of String which are called Command line arguments. So, if you run your program from command line as java ifDemo 10 12, then args will contain [10,12] , args[0] will be 10, args[1] will be 12 and args.length will be 2.

This line is throwing an error because you didn't run the program with command line arguments.

int x= Integer.parseInt(args[0]);

args is empty and you are trying to access the 1st element of the array which causes 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.

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

You are not passing an integer as command line argument int x= Integer.parseInt(args[0]);

args[0] is assigned a value by command line arguement.

Compile and run program in following way:

javac ifDemo.java

java ifDemo 23

When you run the program, args[0] is 23.

Earlier as you were not providing any argument by command line, so args[0] was not set and you were getting an array-outofbound-exception.

Ritesh Kumar Gupta
  • 5,055
  • 7
  • 45
  • 71