• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Got an unexpected out-of-bounds exception

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23965
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic