0

why am i getting an array index out of bounds for this code? i am getting an error exception in thread MainJava.Lang.ArrayIndexOutOfBoundsException

//java calculator
public class Calculator
{
public static void main(String[] args)
{
double a,b,m;
char c;
a=Double.parseDouble(args[0]);//taking input from command line
c=args[1].charAt(0);
b=Double.parseDouble(args[2]);
switch(c)//using switch to perform calc operations
{
case('+'):
           m=a+b;
           System.out.println(args[0]+args[1]+args[2]+"="+m);
           break;
case('-'):
           m=a-b;
           System.out.println(args[0]+args[1]+args[2]+"="+m);
           break;
case('*'):
           m=a*b;
           System.out.println(args[0]+args[1]+args[2]+"="+m);
           break;     
case('/'):
           m=a/b;
           System.out.println(args[0]+args[1]+args[2]+"="+m);
           break;
default:
          System.out.println("invalid option");
}
}

}
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
sally
  • 11
  • 2
  • 2
    Have you passed in any parameters? – Reimeus May 13 '15 at 16:58
  • 1
    Quite clearly, you don't have an `args[0]`/`args[1]`/`args[2]` We don't know which though because you didn't post a stack trace, you just told us the error. – Nicholas Eason May 13 '15 at 17:02
  • 1
    Additionally, please take time to format your code. I assume it doesn't *really* look like that on your screen - use the preview to make sure that your post is readable *before* you post it. – Jon Skeet May 13 '15 at 17:03

4 Answers4

1

The code works fine if you pass it command line parameters.

You can add parameters to the class by right clicking the Calculator.java file inside eclipse and going to run as > run configurations. Inside there you can click on the parameters tab and add the parameters manually to the program arguements.

Alternatively, you can run this on the command prompt and pass the parameters that way.

On command line: java Calculator 5 + 1

More details are available here: https://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

If you didn't intend to use command line arguements, you can use a Scanner object to set the variables a, b and c.

Scanner input = new Scanner(System.in);
a = input.nextDouble();
c = input.next().charAt(0);
Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
0

In general, if you are going to write code that depends on command line input, its good practice to put some checks at the begining. For example,

if (args.length < 3)
    System.out.println("Insufficient Arguments");
else {
    //The rest of your code
}
GreySage
  • 1,153
  • 19
  • 39
0

To elaborate on the comments, it seems likely that your args array has length < 3. To confirm this, add System.out.println("args has length: " + args.length) as the first line of your main function and see what that prints. If the result is anything less than 3, then you're going to get an ArrayIndexOutOfBoundsException when you try to access args[2] (or the others if the length is even less).

More on ArrayIndexOutOfBoundsExceptions here: What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?

From there, the question becomes "what do I do if I don't have enough arguments?". Throwing an exception is probably not the idea answer. Some other options include:

  • Give the variables default variables (as the user didn't provide them), and continue as usual
  • Print a more descriptive error message and terminate
Community
  • 1
  • 1
Mshnik
  • 7,032
  • 1
  • 25
  • 38
0

Java Code Use scanner function to take input.

 public class TestProgram {

        public static void main(String args[]){
            double a,b,m;
            char c;
            System.out.println("Enter the first number");
            Scanner scanner = new Scanner(System.in);
            a=Double.parseDouble(scanner.next());//taking input from command line
            System.out.println("Enter the operation you want to perform + or - or * or /");
            c=scanner.next().charAt(0);
            System.out.println("Enter the second number");
            b=Double.parseDouble(scanner.next());
            switch(c)//using switch to perform calc operations
            {
            case('+'):
                       m=a+b;
                       System.out.println(a +"+"+ b +"="+ m);
                       break;
            case('-'):
                       m=a-b;
                       System.out.println(a +"-"+ b +"="+ m);
                       break;
            case('*'):
                       m=a*b;
                       System.out.println(a +"*"+ b +"="+ m);
                       break;     
            case('/'):
                       m=a/b;
                       System.out.println(a +"/"+ b +"="+ m);
                       break;
            default:
                      System.out.println("invalid option");
            }
            }

    }
Ritesh Karwa
  • 2,196
  • 1
  • 13
  • 17