Results 1 to 20 of 23
- 09-01-2010, 06:34 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
How to solve ArrayIndexBoundOfException error
Hi!
I am a newbie and need some help to know that in the followin' program where i am going wrong:-
public class userinput
{
public static void main(String[] args)
{
System.out.println("Enter the value of A: ");
int a=Integer.parseInt(args[1]);
System.out.println("Enter the value of B: ");
int b=Integer.parseInt(args[2]);
int c=a+b;
System.out.println("The sum is: "+c);
}
}Last edited by amityadav9314; 09-01-2010 at 08:04 PM.
- 09-01-2010, 06:58 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 09-01-2010, 07:03 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
- 09-01-2010, 07:14 PM #4
Can you say why you think you are going wrong? Do you get errors?where i am going wrong
Please copy and paste the full contents of the error message text here.
- 09-01-2010, 07:16 PM #5
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
- 09-01-2010, 07:26 PM #6
Give a man a fish, he eats for a day. Teach a man to fish, he eats for a lifetime.
The idea behind what JosAH said is that you are accessing args[0] and args[1], however if the program is launched with an empty args[] array, you will get this ArrayOutOfBounds exception.
So before your code accesses args[0] and args[1], you will want to check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1].
If you're still having issues implementing this, show us some attempts you've made; and if you still get error messages, posting the FULL error message (copied and pasted) is very helpful.
- 09-01-2010, 07:29 PM #7
When you get errors:
Please copy and paste the full contents of the error message text here.
there is useful information in the error message that you have left off.
- 09-01-2010, 08:03 PM #8
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
amit@ubuntu:~/Desktop/amit$ javac userinput.java
amit@ubuntu:~/Desktop/amit$ java userinput
Enter the value of A:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at userinput.main(userinput.java:6)
amit@ubuntu:~/Desktop/amit$
:
:
:
that is the error i am gettin'....
- 09-01-2010, 08:07 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
So before your code accesses args[0] and args[1], you will want to check if args.length is 2 or more, and if it's not, do not attempt to access args[0] and args[1].
thanks for tryin' to help/explain me but I really can't get what to do?
Please explain it more or correct the error.
- 09-01-2010, 08:15 PM #10
Have you learned the concept of if statements?
Java Code:if (...) { // Something that will happen if ... is true } else { // Something that will happen if ... is not true }
- 09-01-2010, 08:26 PM #11
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
- 09-01-2010, 08:33 PM #12
That is almost the code there. Here is the actual code:check if args.length is 2
args.length == 2
- 09-01-2010, 08:35 PM #13
The idea of this particular if statement is to compare two things: args.length and 2. There are several comparison operators which I'm assuming you're familiar with (<, >, <=, >=, ==, and so on).
Using one of those five operators, you want to check whether or not args.length is greater than or equal to 2. And if it is, you want to execute the rest of your code:
Java Code:// The next four lines should be what executes if args.length is greater than or equal to 2. int a=Integer.parseInt(args[0]); int b=Integer.parseInt(args[1]); int c=a+b; System.out.println("The sum is: "+c);
- 09-02-2010, 02:21 AM #14
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Your program expects command line arguments do you know what they are?
run from command prompt
C:\"userinput.class filepath here" java userinput 34 35
where userinput.class filepath here is the filepath to your .class file and omit the surrounding ""
i.e.
The reason for your ArrayIndexOutOfBounds Exception is that you are trying to access args[] without passing it any values from command prompt therefore args[1] does not exist.Java Code:public static void main(String args[]){ if(args.length==2){ System.out.println(args[0]); // displays 34 System.out.println(args[1]); // displays 35 // Back to your program System.out.println("Enter the value of A: "); // not sure why you have this int a=Integer.parseInt(args[0]); System.out.println("Enter the value of B: "); // not sure why you have this int b=Integer.parseInt(args[1]); int c=a+b; System.out.println("The sum is: "+c); // should be 69 } }Last edited by al_Marshy_1981; 09-02-2010 at 02:22 AM. Reason: curly braces
- 09-02-2010, 09:31 AM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
And please stop asking people to provide you with the code.
- 09-02-2010, 04:40 PM #16
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
Well I neva asked for the code, Sir.
I was just askin' where the hell I was going wrong.
Now I can understand the problem, but still could not managed to implement that. I am still very new to this programming that's why I could not get those words about "command line arguments". Something still is going wrong, but does not matter there are still two more methods to take the user input that i know...
1:-using util Scanner
2:-using buffered reader
I will get to this problem later.
thanks y'all for replying me.
- 09-02-2010, 04:53 PM #17
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Well, you asked a few times for people to correct your code.
If, after trying the solutions provided, you are still having problems then you post your attempts and explain where it is going wrong (ie compilation errors, or runtime exceptions).
- 09-02-2010, 06:35 PM #18
Member
- Join Date
- Sep 2010
- Posts
- 10
- Rep Power
- 0
public class userinput
{
public static void main(String args[])
{
System.out.println("Enter the value of A: ");
int a=Integer.parseInt(args[0]);
System.out.println("Enter the value of B: ");
int b=Integer.parseInt(args[1]);
int c=a+b;
if(args.length>=2)
{
System.out.println("The sum is: "+c);
}
}
}
This is what i did later but still I am gettin' a run time error ans that is:-
"C:\Program Files\Java\jdk1.7.0\bin>java userinput
Enter the value of A:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at userinput.main(userinput.java:8) "
- 09-02-2010, 06:53 PM #19
Please go back through the posts on this thread. The problem causing the error has been described, analyzed and solved several times.
Why do you test if the data in args is present AFTER your code assumes that it is present and tries to use it? Move the if() test to before using the args array elements.
The computer executes the statements in your program in the SAME order that they are entered. It normally starts at the top and moves down. Code at the bottom is done AFTER code at the top.
- 09-02-2010, 07:26 PM #20
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Similar Threads
-
plz solve this error
By silversurfer2in in forum AWT / SwingReplies: 14Last Post: 06-15-2010, 03:30 PM -
Can't Solve Array Error
By Gregadeaux in forum New To JavaReplies: 2Last Post: 12-14-2009, 03:01 AM -
how to solve syntax error
By pro85 in forum Java AppletsReplies: 5Last Post: 04-06-2009, 11:20 AM -
Can't solve error message while looping
By BHCluster in forum New To JavaReplies: 15Last Post: 04-22-2008, 10:51 AM -
Help mi solve my error
By Deon in forum New To JavaReplies: 3Last Post: 01-11-2008, 05:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks