aspose file tools*
The moose likes Beginning Java and the fly likes Question about my for loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about my for loop" Watch "Question about my for loop" New topic
Author

Question about my for loop

Mark Lepro
Greenhorn

Joined: Feb 27, 2012
Posts: 4
Hello!

I'm a Java (and programming) beginner and I'm currently learning how to play with 'for loops'. I have a question concerning my code below. Why is it that it refuses to compile if I try to initialize the variable in the for loop itself? It only works if initialized in the main method. The "weird" thing (from my perspective) is that the "mins" variable doesn't have to be initialized that way (in the main method). What would be the reason?

This is just an exercise where I want to have the time display every round hour.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

it's hard to say why it doesn't work without seeing what exactly you are doing...but my GUESS is that you were trying something like this:


and then getting some kind of unknown variable error.

The problem there would be that teh ming and secs variables only exist inside the for-loops. When you get to the System.out.println, they have fallen out of scope, and thus don't exists anymore.

By declaring them above in your main (like you have it in your post), they have a scope of the entire method, so can be used anywhere in that method.

You may want to google "jave variable scope" (or something like that), or even search this forum for the same topic.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch

Don’t use DecimalFormat if you can use printf() instead. It is a lot better.
You can’t see the inner loops if nothing happens inside them. Be consistent with your loops; you started with < 10, which I think is correct, and then used <=59. Also put more space between token, without too many blank lines. I have changed your tabs to spaces for the indenting. Like this:
I cannot see anywhere that you would have a compiling problem; please explain that more.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
I think Fred has guessed the reson for your errors.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
The error was "The local variable secs may not have been initialized". As Fred says, it's basically about scope, but what's out of scope isn't the variable declaration but the initialization. hrs and mins are okay, since the compiler can see their initialization. But the initialization of secs is hidden inside the second loop. (That's my theory anway; I'm not a compiler expert)
Mark Lepro
Greenhorn

Joined: Feb 27, 2012
Posts: 4
Thank you guys for the replies, I really appreciate.

If I understand correctly, my variable "secs" is sort of forgotten (out of scope) when I only initialize it in the loop statement. I'm still not 100% sure why "mins" doesn't behave the same way. But I'm guessing that it's because my output command is right outside the minutes loop while seconds is nested, hence dropped. Woah I'm not sure I'm making sense Also, sorry if I'm not using all the proper terms.

I compiled the code you gave me Campbell and still get this error:

C:\Javawork>javac nestedLoop2.java
nestedLoop2.java:16: error: variable secs might not have been initialized
System.out.printf("%02d:%02d:%02d%n", hrs, mins, secs); // Changed:
Have got rid of - 60
^
1 error


But the tips you gave me are very valuable, thank you!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Mark Lepro wrote:Thank you guys for the replies, I really appreciate.

If I understand correctly, my variable "secs" is sort of forgotten (out of scope) when I only initialize it in the loop statement.


No.

If it was out of scope, the error message would have been something like "undefined symbol". When you get "might not be initialized," it means you're doing something like this:



or even this:


In both cases, when we get to the println(), the compiler cannot be sure that x has been given a value. In the second case, even though you and I can say, "But if the first condition doesn't get executed, then the second one has to!" the compiler does not do that kind of analysis. Note that if we change the second "else if" to just "else", then the compiler can be sure that exactly one of the paths will be executed.

This kind of behavior can also occur with loop:



Again, if condition is false when we first hit the for or while, then the body will never be executed, and x will not be set.

Note tat the WRONG way to fix this is to just blindly give the variable an initial value to make the compiler happy. We have to examine our logic to see what path we forgot. In some cases, it turns out that the right solution is to give the variable an initial value, but we only do that after we've closely examined our logic, and only if it makes sense for the variable to actually have that value because we're going to use it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11480
    
  16

and to re-iterate...without seeing the EXACT code you are compiling, and the EXACT error you are getting, we are all only guessing as to what the problem/solution is.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Mark Lepro wrote:If I understand correctly, my variable "secs" is sort of forgotten (out of scope) when I only initialize it in the loop statement. I'm still not 100% sure why "mins" doesn't behave the same way. But I'm guessing that it's because my output command is right outside the minutes loop while seconds is nested, hence dropped.


The braces delineate a block's scope. If you moved the system out statement outside the outermost loop, you would get the "not initialized" error for both mins and secs.
 
Consider Paul's rocket mass heater.
 
subject: Question about my for loop