For-loop: initialisation of variable, can't set variable to value zero?

Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

For-loop: initialisation of variable, can't set variable to value zero?

Well, the title says it all but here's a little background info:

I've just begun studying Java from a beginners handbook in my native language. If there already exists a thread on this topic I apologize but I had no idea how to condense this question in a one or two word query for the search function.

So I'm busy with the chapter on loops and one of the exercices asks you to write a piece of code that would draw lines from left to right over a JPanel, all of them intersecting in the middle. You can give the increment between the lines in JTextField and execute the loop for drawing each line with a button or by hitting "enter/return". here's the code for the loop I wrote:

Now initially I set the value of increment2 as zero, but this had the effect that when I ran the complete code in eclipse, I just got a JFrame that froze, without the JPanel. I tinkered and cursed and tried a bunch of different things, to no avail. After staring at my screen with growing frustration for a while I started fixing my gaze on that zero, thinking about the fickle nature of this number throughout the history of mathematics and decided to eliminate any problems concerning zero (i know, pretty far fetched but after an hour or more of pulling your hair and cussing at the screen I was willing to try anything). I wasn't really expecting much but, what do you know... it worked! I had to make a modification of -1 to the original value of variable midden to keep the position of the lines in check with the exercice but all in all I was glad I got the damn thing working, albeit not in a very elegant way.

So much for the background info, back to the question now. Is there a reason why this variable can't initially be set to the value of zero and if yes, wich is it?

The value of increment2 before the start of the for loop you posted won't affect the behaviour of that loop one way or the other. That's because the loop includes the initialisation statement "increment2=0".

Perhaps you could post a compilable example? And indicate the line(s) you changed and say precisely how you changed them. (or post both versions if that is easier.)

The functioning code is identical to the one above, with the exception that in the method setParameters() you parenthesize the value of variable "midden" and append "-1" to it; and in the initialization of the for-loop you substitute "0" with "1".