• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayIndexOutOfBoundsException that I just can't figure out.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been working on this project and it is all done except for the reporting and printing of the outcomes, but i continue to get this error that I just can't seem to understand why:




The commented out section prints all the correct info but the for loop above that should do the same and does not. I get it on each one of the lines: names, table, rowsum.

Can anyone see the problem?


[HENRY: Added Code Tags]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for (col = 0; col < 3; ++col);

The loop will iterate through values of col and do nothing as there is a semicolon at the end. The value of col at the end causes your exception in the following statements.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is very difficult to read without nicely indented formatting. Please use code tags (use the "Code" button in the editor) in the future.

Your for loops have a stray semicolon after the parens that are causing behavior you do not expect (See the arrow below where I point out where the semicolon is in your for loops. Because of the semicolon, the code in the curly braces is not being executed as part of the for loop, instead it would be executed after the for loop.

for ( ... ) ; <--



Edit: Ack! I must have gone for a coffee break while I posted this, Amit posted an answer before mine posted.
 
Amit Bhargava
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heh, forgot to add the part about the [code] tag. Thanks, Carol!
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using 2 or 3 in the first place? You ought not to use number literals (except 0 1 and -1), but fields. In this case you should use the length field of the array
 
Chris Argueta
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys! That's exactly what was happening the semicolons were killing my for loop. I took them out and there everything went. I was my first time posting so my apologizes for the sloppy code posting. I appreciate the assistance its exactly what I needed. Thanks again!

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would have been better to declare the counter variable as local to the loop. It doesn’t usually belong outside the loop, and in this case you would have suffered a compiler error.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic