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.
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.
Joined: Oct 13, 2005
I think Fred has guessed the reson for your errors.
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)
Joined: Feb 27, 2012
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:
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
But the tips you gave me are very valuable, thank you!
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.
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.
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.
Joined: Mar 12, 2011
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.