Got an unexpected out-of-bounds exception
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Based on Chapter 10 Sierra/Bates, Question 2
When I run program with java _ c A., I expect the output to be c A.. My understanding is that the index of input starts at 0, i.e.
_A_V_[0] is c
_A_V_[1] is A
_A_V_[2] is .
Yet, I get the following exception:
C:\JavaJava>java _ c A.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at _.main(_.java:6)
1. Why do I get this exception?
2. How do I correct code such that it outputs c A.
When I run program with java _ c A., I expect the output to be c A.. My understanding is that the index of input starts at 0, i.e.
_A_V_[0] is c
_A_V_[1] is A
_A_V_[2] is .
Yet, I get the following exception:
C:\JavaJava>java _ c A.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at _.main(_.java:6)
1. Why do I get this exception?
2. How do I correct code such that it outputs c A.
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
posted 15 years ago
Basically, you incremented the index too early -- you did it in the condition. This means that the body will use an index which is one too large. Move the index increment to the reinitialization of the for-loop, and it will work.
Henry
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Basically, you incremented the index too early -- you did it in the condition. This means that the body will use an index which is one too large. Move the index increment to the reinitialization of the for-loop, and it will work.
Henry
posted 15 years ago
Incrementing and checking for a condition at the same time can lead to disasters. Try moving x++ to the next section of the for loop.
Two things you can do
1. Print the value of x for each loop.
2. Double check your params. Are you passing "c A." or "c A .". Notice the extra space in the second call
[EDIT]
Henry beat me to it by 3 mins !
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Incrementing and checking for a condition at the same time can lead to disasters. Try moving x++ to the next section of the for loop.
Two things you can do
1. Print the value of x for each loop.
2. Double check your params. Are you passing "c A." or "c A .". Notice the extra space in the second call
[EDIT]
Henry beat me to it by 3 mins !
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Look at this line:
Initially x was 0, then condition is checked:as 0<3, loop runs 1st time, x is incremented by 1(x becomes 1) and then the body of loop runs adding _A_V[1] to $,
then condition is checked: as 1<3, loop runs 2nd time, x is incremented by 1(x becomes 2) and then the body of loop runs adding _A_V[2] to $,
then condition is checked: as 2<3, loop runs 3rd time, x is incremented by 1(x becomes 3) and then the body of loop runs adding _A_V[3] to $,as max index is 2, _A_V[3] results in out of bound exception,,,,,
Hope you understood....
You can check that the increment is happenning before the body of loop executes by modifying your code like this:
when c A . is passed as command line arguments the output is :
1
A
The solution to this problem is fairly simple:
Initially x was 0, then condition is checked:as 0<3, loop runs 1st time, x is incremented by 1(x becomes 1) and then the body of loop runs adding _A_V[1] to $,
then condition is checked: as 1<3, loop runs 2nd time, x is incremented by 1(x becomes 2) and then the body of loop runs adding _A_V[2] to $,
then condition is checked: as 2<3, loop runs 3rd time, x is incremented by 1(x becomes 3) and then the body of loop runs adding _A_V[3] to $,as max index is 2, _A_V[3] results in out of bound exception,,,,,
Hope you understood....
You can check that the increment is happenning before the body of loop executes by modifying your code like this:
when c A . is passed as command line arguments the output is :
1
A
The solution to this problem is fairly simple:
Waiting for enlightenment....
Sandra Bachan
Ranch Hand
Posts: 434
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Wow, this question is so tricky, yet so simple. Bet they would include this on the exam as the test-takers are under time constraints.
Thanks for all the feedback!
Thanks for all the feedback!
Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
| Of course, I found a very beautiful couch. Definitely. And this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |












