6 Replies - 1498 Views - Last Post: 29 January 2010 - 07:14 PM Rate Topic: -----

#1 javasun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-January 10

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 12:19 PM

Hi,i'm trying to make a simple calculator, i've no comilation errors ,but when i run the program i've this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Calc.main(Calc.java:7)

i understand that my problem is in the array but i don't know why, i wanted to pass the arguments through the main, here u r my code:
class Calc
{
  public static void main(String[] args)
 	{
	   	
	   		
	  int num1=Integer.parseInt(args[0]);
	  char oper=args[1].charAt(0);
	  int num2=Integer.parseInt(args[2]);
	  int res=0;
		  
			  if(oper== '+'){ res=num1+num2;}
		  else if(oper=='-'){res=num1-num2;}
		  else if(oper=='*') {res=num1*num2;}
		  else if(oper=='/') {res=num1/num2;}
		System.out.println(res);
	
	}

}  
	


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

#2 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 12:25 PM

Are you passing arguments or are you just running the program? The error that you are getting is caused by using either a negative number as the index of the array, or by using a number that is larger than the number of elements minus one in the array. My assumption is that you are not passing it any information and therefore, the array has no elements. So when you try to call args[0], there isn't anything there.

Just make sure you are passing it some arguments :)
Was This Post Helpful? 1

#3 javasun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-January 10

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 12:33 PM

View PostxTorvos, on 29 Jan, 2010 - 11:25 AM, said:

Are you passing arguments or are you just running the program? The error that you are getting is caused by using either a negative number as the index of the array, or by using a number that is larger than the number of elements minus one in the array. My assumption is that you are not passing it any information and therefore, the array has no elements. So when you try to call args[0], there isn't anything there.

Just make sure you are passing it some arguments :)


thanks a lot i forgot that i've to pass the parameters at the same line of the run : java Calc 3 + 4, i wrote just the run command and expected to add 3 + 4 after :blink:
Was This Post Helpful? 0
  • +
  • -

#4 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 12:47 PM

No problem, I've forgotten this tons of times. It almost always will end up being the same type of error.

P.S. Don't forget to click the "This post was helpful" link if I helped you out. ;)

This post has been edited by xTorvos: 29 January 2010 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#5 javasun  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-January 10

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 04:24 PM

View PostxTorvos, on 29 Jan, 2010 - 11:47 AM, said:

No problem, I've forgotten this tons of times. It almost always will end up being the same type of error.

P.S. Don't forget to click the "This post was helpful" link if I helped you out. ;)


sure u helped me :) , i clicked it,thanks
Was This Post Helpful? 0
  • +
  • -

#6 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 05:18 PM

Defensive programming tip. :)

Always check that input arguments exist before using them:
public static void main(String[] args)
{		
	if (args.length != 3) {
		System.out.println("Usage: 3 arguments required - number operator number");
		return; // exit program
	}

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

	char oper = args[1].charAt(0); <<<<<<<< should be charAt(1)

	int num2 = Integer.parseInt(args[2]);
...


Was This Post Helpful? 1

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,359
  • Joined: 27-December 08

Re: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsEx

Posted 29 January 2010 - 07:14 PM

View Postn8wxs, on 29 Jan, 2010 - 08:18 PM, said:

Defensive programming tip. :)

Always check that input arguments exist before using them:


I couldn't agree more. :^: Better to validate before hand than to have your input cause an error later in your program.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1