aspose file tools*
The moose likes Beginning Java and the fly likes for loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "for loop question" Watch "for loop question" New topic
Author

for loop question

David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
hi,

new to java so go easy!!!

i have a for loop that does some calculations then jumps back to the main program, what i want is when it jumps back to the main program to icrement an int called y by 1 and then I want it to go back into the for loop again. I have got the for loop working fine it is just the increment by 1 i am struggling with, i think because i have declared y=0 in the main program it keeps getting reset after the for loop has finished, any ideas?
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Can you embed the peice of code that create the problem. Then it would look fine for others to understand the problem.


Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10911
    
  12

you could try another for loop


this would call the inner for loop once with a y value of 0. When the inner loop ends, you go through the outer loop again, so y would now be 1. You can adjust this to your needs.

alternatively, you could move your loop to a method, and call it twice:

this has advantages and disadvantages as well, but it is an option.
[ October 23, 2008: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David McHugh
Greenhorn

Joined: Oct 01, 2008
Posts: 23
cheers fred, just what i needed..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: for loop question
 
Similar Threads
Explanation needed on Break and continue questions
stars
Be The JVM (Head First)
Probably simple code needed translated into English for me
Understanding continue...label do...while