aspose file tools*
The moose likes Beginning Java and the fly likes ArrayIndexOutOfBoundsException that I just can't figure out. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutOfBoundsException that I just can Watch "ArrayIndexOutOfBoundsException that I just can New topic
Author

ArrayIndexOutOfBoundsException that I just can't figure out.

Chris Argueta
Greenhorn

Joined: Feb 26, 2012
Posts: 2
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]
Amit Bhargava
Greenhorn

Joined: May 20, 2011
Posts: 20
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.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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

Joined: May 20, 2011
Posts: 20
Heh, forgot to add the part about the [code] tag. Thanks, Carol!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Feb 26, 2012
Posts: 2
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
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
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.
 
 
subject: ArrayIndexOutOfBoundsException that I just can't figure out.