• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to add values under different if statements  RSS feed

 
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure if I'm breaking any rules and apologize if I'm doing so. My initial question had to do with looping but now I'm having trouble
adding certain variables, so thought it might be more appropriate to post her.
The original thread is here: http://www.coderanch.com/t/632911/GUI/java/looping-JOptionPane

If you look at the end of the code it won't recognize the "overtime" and "hourDiff". I have to use it for each level the
user chooses and am not sure how to do so. Even if I put it under the if{ statement it won't calculate. I assume it's due
to it being cut off from other blocks but I'm not sure how to combine them.

My code is:

 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

First of all, you have to fix all the parenthesis and indentations. It makes it very hard to read otherwise.

To answer your question, those two variable are not in scope at the time you are trying to use them. And BTW, for this example it was very easy to tell, but for more complicated examples, proper indentations really help too.

Henry
 
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry pointed out, the variables are out of scope where you are trying to use them. Since you are declaring them inside of an IF statement, the variable only lives inside that code block (the IF statement in this case). If you want to use the variable elsewhere you will need to declare it at the top of your main() method. Really it can be declared anyway inside the main method as long as it's not inside of another code block like and IF statement or loop.

For ease of explanation, you can move the declaration to the top of you main method between line 8 and 15.

Example:

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mmm...I think I understand what both of you are saying and I appreciate it. I've moved the if{ and defined the variables in the main
but it's still giving me the same error, where in the first if{ statement it says both totalHours (line 20) and later in the program
that hourDiff (line 78) cannot be resolved to a variable or initialized. Am I missing something pretty simple?

 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, local variables must be initialized before they're used. Unlike instance variables they do not have default values. At line 20 totalHours has not been initialized, so to use it in an expression at line 20 is illegal. The hourDiff variable has much the same problem, it will only conditionally get initialized, which isn't good enough.

By the way, as Henry already asked, please take care to post properly indented code, because it's much more readable. Make it easy as possible for people to help you
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error message for totalHours is: variable totalHours might not have been initialized
The error message for hourDiff is: variable hourDiff might not have been initialized
Aren't they self-explanatory?

You really should fix indentation in your code.
And try to declare your variables as close to the place you are using them as possible.
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured it out. Thanks for everyone's help and sorry about the indentation.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I missed the initialization in my example also. My brain was elsewhere.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!