• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is my for loop not running?  RSS feed

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following section of code is supposed to print out markings around my clock face. It currently prints out one but no more.
I have added a drawString() to print out the position and it is always on 1. Have i missed something obvious here . . . it all compiles and runs with no errors, it just doesn't seem to loop.

 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a print statement inside the loop. You may be seeing twelve lines in the same location, rather than one line.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, i have used And changed the position it prints to on each loop. I just get test printed once . . .
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:The following section of code is supposed to print out markings around my clock face. It currently prints out one but no more.


Your declaration of the variable "position" is inside the loop, reseting itself whenever the loop counter "i" increments. From what I see, this position variable should act as your loop counter, but for loops don't that double for the counter.

What you may want to do assign the variable position to i inside the loop.

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are initializing position to value 1 each time you enter loop, so you're always getting the same result inside of it. Put the initialization before loop, and then check if it works.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have definitely got 12 lines in the same location.
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:
tom davies wrote:The following section of code is supposed to print out markings around my clock face. It currently prints out one but no more.


Your declaration of the variable "position" is inside the loop, reseting itself whenever the loop counter "i" increments. From what I see, this position variable should act as your loop counter, but for loops don't that double for the counter.

What you may want to do assign the variable position to i inside the loop.



Thank you K. Tsang, the reason my drawString was only printing once was the same reason. All is working now though and it looks great, thanks.
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
tom davies wrote:Yes, i have used . . .
That isn’t a print statement. This is:-
 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
tom davies wrote:Yes, i have used . . .
That isn’t a print statement. This is:-


Because the class i am using does not have a main method, i am using them to add components to my jPanel, i didn't think a System.out.print() line would work. I thought to add text to a JPanel it has to be done via drawString().
 
Campbell Ritchie
Marshal
Posts: 55789
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you start your app from the command line or terminal, that is where System.out will be directed as a default. If you use an IDE, there is usually a console window on the IDE where such messages appear.

As an alternative, you could use JOptionPane, but that has modal dialogues, which will slow your app down as you close all 12 of them.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!