Take the 2-minute tour ×

I'm currently still learning java so this is probably just an amateur mistake

The intention was that the 'for' loop would draw out an array of 'Bars' onto a bar chart given different heights (apologies for the poor formatting)

static int[]cwMarks = new int[4];
static int[]examMarks = new int[4];
static int barWidth = 4; //Standard bar width
private static int posX = -15; //Decides X position of bar
private static int posY= 210; //Decides Y position of bar
static int barCount = 8; //Tracks number of bars needing to be drawn
static Bar[] barArr = new Bar[barCount]; //Array storing Bars

    examMarks[2]= 20;
    examMarks[3]= 30;

public static void draw()

    for (int i=0,j=0;i<barCount;j++) //i represents the element of barArr, j represents element of cwMarks/examMarks

        barArr[i].changeSize(barWidth, cwMarks[j]);

        barArr[i].changeSize(barWidth, examMarks[j]);





I assumed that this would created 4 pairs of 2 bars, but instead the loop only seems to execute it's code twice (producing 2 pairs of 2). This is what is drawn:

enter image description here

Any ideas as to why this is?

share|improve this question
Are you sure it's not drawing 8 bars? 1 up, 1 down (at the same x-axis point), 2 up, 2 of size 0, and 2 up is 8 bars. –  MadConan Nov 6 '13 at 20:44
Have you thought about using a debugger? or at least add some system.out's inside your loop? –  A4L Nov 6 '13 at 20:45
oh, and you are doubling the value of i each time through the loop. –  MadConan Nov 6 '13 at 20:45
please insert something like System.out.println("Created bar "+i+" at "+<horizontal_position>+", "+<vertical position>); before each i++ where those positions are the ones from moveHorizontal and moveVertical. This should help understand if the loop is running correctly. –  zebediah49 Nov 6 '13 at 20:45
run it through a debugger, or print out something inside the loop so you can see where you are. –  Andrew Nov 6 '13 at 20:45
show 6 more comments

3 Answers


look at it. J++? Shouldn't it be:

for (int i=0;i<barCount;i++)
share|improve this answer
He does define in the comment next to the for() loop what j is for. –  Galen Nare Nov 6 '13 at 20:48
Well, he does increment i twice in the body of the loop. Not a good practice. –  Steve11235 Nov 6 '13 at 20:53
Not good practice, but not incorrect logic. –  zebediah49 Nov 6 '13 at 21:00
add comment

Change posX=+5 to posX+=10 (or posX+=15 depending on how you want it to look). You're code has 2 errors:

  1. You are setting posX to 5 each time instead of incrementing it.
  2. You are incrementing it by too small of a number. Even with fixing the first error, during the first loop, you draw bars at xPos -15 and -10, then you increment xPos to -10 and draw bars at xPos -10 and -5 (See: you've drawn -10 twice already)
share|improve this answer
Yeah, this is it, the compiler is probably interpreting posX=+5 as posX=5 which makes sense as it is a "positive 5". That's why it still compiled. –  fedeetz Nov 6 '13 at 21:00
Nope, I changed it and the 4 bars all clumped together instead of Bar 2 and 3 being spaced apart (seemingly it just ignored it) –  AJCol Nov 6 '13 at 21:11
Have you tried my updated solution yet? It should work now. –  quazzieclodo Nov 7 '13 at 16:03
add comment

No wonder you get a lot of views with no answers. I can't tell, either. Here's a couple of tips.

Put your code in a method instead of making everything static. Try public void run() {} if nothing else. Create an instance in main and invoke run.

Don't update the variables in for loop outside the loop. In a larger loop, that would be confusing.

Try using two for loops instead of the kloogie you have now.

Test the loop logic by removing the graphics code and just doing System.out.println() with meaningful messages.

share|improve this answer
add comment

Your Answer


