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.
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.
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.
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.
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.