• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question about my for loop

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Fred has guessed the reson for your errors.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
What are your superhero powers? Go ahead and try them on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic