wood burning stoves*
The moose likes Beginning Java and the fly likes Using a while loop? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using a while loop?" Watch "Using a while loop?" New topic
Author

Using a while loop?

Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
By looking at the code below, could someone please tell me I can't use the value 'i' outside the while loop. I really need to! How do i go about this?
You shouldnt need to know what the code does, except for that I just want to use final value of 'i'.


[ February 07, 2006: Message edited by: Sam Bluesman ]

Moosey knows best
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I'm wondering about your logic (counter==numberOfActivities ?!) but i's scope allows you to use it after the while loop, since it was desclared before the while loop:


There is no emoticon for what I am feeling!
Sam Bluesman
Ranch Hand

Joined: Nov 21, 2004
Posts: 191
but if i place a System.out.println(i) outside the while loop but still in the for loop nothing happens??
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
"Nothing happens" is different from a syntax error. It sounds like you have logical errors. Here's a demo that shows i can be used as I suggested.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39

Originally posted by Sam Bluesman:
but if i place a System.out.println(i) outside the while loop but still in the for loop nothing happens??


Two possible scenarios for this... either the for loop never executes or the while loop never finishes.

Put debugging messages before and after the for loop. Put debugging messages before and after the while loop. This should give you a hint as to what is going on...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Using a while loop?
 
Similar Threads
loops & labels
doubt in servlet
for and while loops
Forwarding control from Servlet to JSP
create a hand of cards