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

• June 10th, 2012, 06:04 PM
Bitbot
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:

for (increment2 = 1; midden + increment2 <= hoogte; increment2 = increment2 + increment)
{
g.drawLine(0, midden + increment2, breedte, hoogte - increment2);
}

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?
• June 10th, 2012, 06:57 PM
pbrockway2
Re: For-loop: initialisation of variable, can't set variable to value zero?
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.)
• June 10th, 2012, 08:45 PM
Bitbot
Re: For-loop: initialisation of variable, can't set variable to value zero?
I'll just put the "before and after" versions of my panel class, I think that wil indeed be easier.

some info: I'm running Eclipse Platform Version: 3.7.1 on Fedora core 16. Don't know if that matters, but hey...
I compile and run this code in eclipse.

This is the code to generate the JFrame in wich the action takes place:

import javax.swing.*;

public class DiagonaleLijnen extends JFrame
{
public DiagonaleLijnen()
{
JFrame venster = new JFrame();
venster.setTitle("Diagonale Lijnen");
venster.setSize(500,400);
venster.setLocation(100, 100);
venster.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel hoofdpaneel = new DiagonaleLijnenPaneel();
venster.setVisible(true);
}

public static void main(String[] arg)
{
new DiagonaleLijnen();
}
}

And then this is the original (buggy) code for the JPanel where the increment is set by the user and the lines are drawn, but when run it gives a frozen, empty JFrame:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DiagonaleLijnenPaneel extends JPanel implements ActionListener
{

private JTextField invoervak;
private JButton tekenknop;
private int hoogte, breedte, midden, increment, increment2;

public DiagonaleLijnenPaneel()
{
invoervak = new JTextField("3",4);
tekenknop = new JButton("Teken lijnen");
add(new JLabel("Geef de gewenste interval: "));
}

public void setParameters()

{
hoogte = getHeight();
breedte = getWidth();
midden = hoogte / 2
increment = Integer.parseInt(invoervak.getText());
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLACK);
for (increment2 = 0; midden + increment2 <= hoogte;
increment2 = increment2 + increment)
{
g.drawLine(0, midden + increment2, breedte, hoogte - increment2);
}
}

public void actionPerformed(ActionEvent e)
{
setParameters();
repaint();
}

}

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".
• June 13th, 2012, 09:27 PM
Bitbot
Re: For-loop: initialisation of variable, can't set variable to value zero?
• July 15th, 2012, 02:32 PM
jps
Re: For-loop: initialisation of variable, can't set variable to value zero?
Quote:

Originally Posted by Bitbot