File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Got an unexpected out-of-bounds exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Got an unexpected out-of-bounds exception" Watch "Got an unexpected out-of-bounds exception" New topic
Author

Got an unexpected out-of-bounds exception

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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.


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5



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 !


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Prateek Rawal
Ranch Hand

Joined: Dec 19, 2009
Posts: 90
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:


Waiting for enlightenment....
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Got an unexpected out-of-bounds exception