Using if-else statement in Java

Discussion in 'Mac Programming' started by crtvmac, Apr 10, 2010.

  1. crtvmac Guest

    Joined:
    Aug 14, 2009
    #1
    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)
     
  2. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #2
    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.
     
  3. Eraserhead macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #3
    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);
            }
    	
       }
    
    }
    
     
  4. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #4
    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 ... 
    
    }
    
     
  5. mrbash macrumors 6502

    Joined:
    Aug 10, 2008
    #5
    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.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
  7. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #7
    Thank you for the assistance. The web page was exactly what I needed to understand how to pass arguments.
     
  8. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #8
    Thanks for the suggestion. I will use more spacing for my coding in the future.
     
  9. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #9
    Thanks for the assistance. I now fully comprehend how to pass arguments.
     
  10. crtvmac thread starter Guest

    Joined:
    Aug 14, 2009
    #10
    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.
     

Share This Page