Results 1 to 11 of 11
Thread: ArrayIndexOutofBounds Exception
- 04-09-2010, 12:33 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
ArrayIndexOutofBounds Exception
Hi,
I am new to java..
please help...
public class atul{
public static void main(String[] args) {
System.out.println("Addition of two numbers!");
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a + b;
System.out.println("Sum: " + sum);
}
}
Its giving the "ArrayOutofBounds Exception". Pls help..
I am using JDK 1.6
- 04-09-2010, 12:43 PM #2
I believe this is because your array is undefined, you have no element in args[0] and args[1].
Carpe Diem
Each day's a gift and not a given right
- 04-09-2010, 01:22 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Yep.
What's the command you're calling?
"java atul" then what?
- 04-09-2010, 01:35 PM #4
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
Java Code:public class atul() { public static void main(String[] args) { //Define array of type int //Add values to the array System.out.println("Addition of two numbers!"); int a = args[0]; int b = args[1]; int sum = a + b; System.out.println("Sum: " + sum); } }Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 04-09-2010, 01:52 PM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Before you execute a program that requires user to input command line parameters, it's always a good idea to add a check to see if they were entered correctly, for example:
Java Code:public class CmdPar { public static void main(String[] args) { if(args.length == 0) { System.out.pritln("Usage: java CmdPar param1 param2"); System.exit(1); } //execute the program now that you know args is initialized }
- 04-09-2010, 02:05 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 04-09-2010, 03:03 PM #7
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
- 04-09-2010, 03:11 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Still doesn't solve his problem, which is that "args" is either empty or has only one argument in it.
- 04-09-2010, 03:19 PM #9
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
- 04-09-2010, 03:48 PM #10
if you expect exactly to numbers as arguments then use
the parse method can throw an exception so that's why you should use a try-catch block.Java Code:import java.text.ParseException; public class atul { public static void main(String[] args) { if (args.length != 2) { System.out.println("Usage: java atul number1 number2"); System.exit(1); } try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); } catch (NumberFormatException nfe) { System.out.println("You entered invalid numbers, retry."); } } }Last edited by j2me64; 04-09-2010 at 03:57 PM.
- 04-10-2010, 10:47 AM #11
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
ArrayIndexOutOfBounds
By SwEeTAcTioN in forum New To JavaReplies: 6Last Post: 12-07-2009, 12:59 AM -
exception
By mohneesh in forum New To JavaReplies: 3Last Post: 08-16-2009, 02:28 PM -
Exception!
By rameshraj in forum Advanced JavaReplies: 1Last Post: 05-05-2008, 01:39 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks