aspose file tools*
The moose likes Beginning Java and the fly likes For loops problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "For loops problem" Watch "For loops problem" New topic
Author

For loops problem

Terence hiu
Ranch Hand

Joined: Oct 21, 2012
Posts: 36
This might be a simple question to you guys, but im having difficulties with this.
Why won't this for loop print out 1 to 31 as on dayInMonth array? but instead it only goes up to 12?



Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Your for loop is totally different from what you want. This code should produce ArrayIndexOutOfBoundsException after printing value 12.

What you want to do in for loop is initialize counter (say your x) to go from 0 to the last index value of an array (note that you can get length of an array with array.length). Then inside your loop you would access and print out value that is stored in the array at position (index) specified by x, not x + 1.

Also, please read UseCodeTags (<-click) and edit your post accordingly so the code will be easier to read.
And, welcome to the Ranch!


The quieter you are, the more you are able to hear.
Terence hiu
Ranch Hand

Joined: Oct 21, 2012
Posts: 36
Thank you for your help, I finally solved the problem.
Thank you very much!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39861
    
  28
I have added code tags, and you can see how much better it looks. Why are you returning null? That is a potentially dangerous practice. That method ought to have void for its return type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: For loops problem