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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default 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.)

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.add(hoofdpaneel);
    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: "));
    add(invoervak);
    invoervak.addActionListener(this);
    add(new JLabel(" pixels"));
    add(tekenknop);
    tekenknop.addActionListener(this);
    }

    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".

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    No one willing to answer?

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

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

    Quote Originally Posted by Bitbot View Post
    No one willing to answer?
    OR...
    no one willing to stumble over text format code... I sure scrolled past all of it

Similar Threads

  1. Setting variable correctly in sentinel-controlled loop
    By exchaoordo in forum Loops & Control Statements
    Replies: 1
    Last Post: June 1st, 2012, 03:40 PM
  2. [SOLVED] Assign a loop to variable to compare strings
    By norske_lab in forum Loops & Control Statements
    Replies: 3
    Last Post: March 6th, 2012, 02:46 PM
  3. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  4. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 05:34 AM
  5. Replies: 1
    Last Post: October 16th, 2010, 03:32 PM