Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
I just switch from BlueJ IDE to presently using the Eclipse IDE. I think it is much better. I am just starting to learn how to program in Java, reading 'Osborne Teach Yourself JAVA'. Now to my question. For some reason, probably not programming enough yet, I am not understanding what to do when I run my program and get this error message:

HTML:
'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at ZeroDivide.main(ZeroDivide.java:12)'

Will someone please give me an example, by showing me the rest of the program coding, I am leaving out as it pertains to the program in this thread? I tried writing code both ways below. I still am not programming correctly to get the program example I've submitting in this thread to give me output without the error message. The first program coding is how the book illustrate. The second example is my coding attempt to run the program without getting the error message.

/**
* Write an application that accepts two command-line arguments.
* Convert these to double values and divide the first number by
* the second number. Use an if-else statement to prevent division
* by zero from occurring.
*
*
*/

HTML:
public class ZeroDivide {
  public static void main(String args[]){
        double d1 = Double.valueOf(args[0]).doubleValue();
	double d2 = Double.valueOf(args[1]).doubleValue();
	if(d2 == 0) System.out.print("Cannot divide by zero");
	else System.out.print("Answer is:" + d1/d2);
	
   }

}

I also tried this program coding method, and got the following message:

/**
* Write an application that accepts two command-line arguments.
* Convert these to double values and divide the first number by
* the second number. Use an if-else statement to prevent division
* by zero from occurring.
*
*
*/
HTML:
public class ZeroDivide {
  public static void main(String args[]){
        double d1 = Double.valueOf(args[0]).doubleValue();
	double d2 = Double.valueOf(args[1]).doubleValue();
	if(d2 == 0){ System.out.print("Cannot divide by zero");
	}else{ System.out.print("Answer is:" + d1/d2);
	}
   }

}

HTML:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
	at ZeroDivide.main(ZeroDivide.java:12)
 
You should also space your code out more, so its more easily readable.

Code:
public class ZeroDivide
{
  public static void main(String args[]){

        double d1 = Double.valueOf(args[0]).doubleValue();
	double d2 = Double.valueOf(args[1]).doubleValue();

	if(d2 == 0)
        {
            System.out.print("Cannot divide by zero");
        }
	else
        {
            System.out.print("Answer is:" + d1/d2);
        }
	
   }

}
 
For some reason, probably not programming enough yet, I am not understanding what to do when I run my program and get this error message:

Code:
'Exception in thread "main" java.lang.[B]ArrayIndexOutOfBoundsException[/B]: 0
	at ZeroDivide.main(ZeroDivide.java:12)'

An ArrayIndexOutOfBoundsException means what the name suggests: you are trying to access an element of an array that doesn't exist. It's like you have three boxes, and I come ask you for the fifth one. You'd be confused too. ;)

Note that the error also tells you where in the code to look (ZeroDivide.java, line 12). A good text editor will show you the line numbers somewhere on a status bar so you can jump right to it.

As someone else mentioned already, it looks like the reason you're getting this error is because the program is expecting command line arguments and you're not giving it any (or if you are, you haven't mentioned it). For example, to run the program and divide 10 by 2, you'd type, from a command line:

java ZeroDivide 10 2

Any parameters you provide (in this case, 10 and 2) get stored in an array called args[] which is passed to the main() method.

Best practices for writing code that expects a certain number of arguments, is to check to ensure the user supplied the correct number, and if they didn't, exit with an error message, like this:

Code:
public static void main(String args[]) {
   // need minimum of two arguments for this to work
   if (args.length < 2) {
      System.out.print("You need to provide two numbers!");
      System.exit(0);
   }

   // ... keep going ... 

}
 
'Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at ZeroDivide.main(ZeroDivide.java:12)'

This is almost the same error that someone else on this thread had. They were using the BlueJ IDE and did not include the additional command-line parameters because they were executing their app via the GUI, not command-line.

The above error explicitly says that you don't have any command-line parameters when you are executing your class.
 
The code is functional, but you aren't supplying any command-line arguments to the program when you run it. That is what it is complaining about.

http://www.cs.colostate.edu/helpdocs/eclipseCommLineArgs.html may be of interest to you.

You may also want to consider using System.out.println if you want to print something and then continue on a new line afterwards.

Thank you for the assistance. The web page was exactly what I needed to understand how to pass arguments.
 
You should also space your code out more, so its more easily readable.

Code:
public class ZeroDivide
{
  public static void main(String args[]){

        double d1 = Double.valueOf(args[0]).doubleValue();
	double d2 = Double.valueOf(args[1]).doubleValue();

	if(d2 == 0)
        {
            System.out.print("Cannot divide by zero");
        }
	else
        {
            System.out.print("Answer is:" + d1/d2);
        }
	
   }

}

Thanks for the suggestion. I will use more spacing for my coding in the future.
 
An ArrayIndexOutOfBoundsException means what the name suggests: you are trying to access an element of an array that doesn't exist. It's like you have three boxes, and I come ask you for the fifth one. You'd be confused too. ;)

Note that the error also tells you where in the code to look (ZeroDivide.java, line 12). A good text editor will show you the line numbers somewhere on a status bar so you can jump right to it.

As someone else mentioned already, it looks like the reason you're getting this error is because the program is expecting command line arguments and you're not giving it any (or if you are, you haven't mentioned it). For example, to run the program and divide 10 by 2, you'd type, from a command line:

java ZeroDivide 10 2

Any parameters you provide (in this case, 10 and 2) get stored in an array called args[] which is passed to the main() method.

Best practices for writing code that expects a certain number of arguments, is to check to ensure the user supplied the correct number, and if they didn't, exit with an error message, like this:

Code:
public static void main(String args[]) {
   // need minimum of two arguments for this to work
   if (args.length < 2) {
      System.out.print("You need to provide two numbers!");
      System.exit(0);
   }

   // ... keep going ... 

}

Thanks for the assistance. I now fully comprehend how to pass arguments.
 
This is almost the same error that someone else on this thread had. They were using the BlueJ IDE and did not include the additional command-line parameters because they were executing their app via the GUI, not command-line.

The above error explicitly says that you don't have any command-line parameters when you are executing your class.

That was me. Thought I understood what I was instructed in my previous thread about passing arguments. I was able to pass arguments as instructed for my last program. I now see I didn't fully comprehend passing arguments as I thought. Now with me doing more programming to hone my knowledge of programming, I now fully comprehend what I was taught.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.