Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Got an unexpected out-of-bounds exception

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20902
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 !
 
Prateek Rawal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic