Re: Very basic help needed
Can you show where you are trying to use the variable that is not working?
Also please use [code=java][/code] so the code gets a colorful highlight for my old eyes.
Re: Very basic help needed
i edited the statement and threw in a big wad of text, please if read through :)
Re: Very basic help needed
Quote:
Originally Posted by
goochasauras
i edited the statement and threw in a big wad of text, please if read through :)
Reading it now, as a side note, see this link as I read your "big wad of text".
Re: Very basic help needed
Quote:
Originally Posted by
goochasauras
R or C is input earlier and then the other input is retrieved when calc_rectangle/calc_circle is called and it
returns a value and assigned to p1area...once i get that assigned back in the if-statement above this comment i want to
use p1area outside of the if-statement in a JOptionPane.showMessageDialog() with some other details. When i try to use
it outside of the if-statement it says the local variable may not have been initialized
If I understand your text, all you have to do is pass that value to a method. This link may help.
If not post the code where you are trying to use the variable and getting the error message.
Re: Very basic help needed
I added another line of code as an example of how I want to use it...it's at the bottom of Main()...but i get red squigglies saying the local variable may not have been initialized
Re: Very basic help needed
If you look at every possible path through the code, when you get to that point there is a chance a variable was not initialized.
A scenario the compiler is complaining about.
What happens if the input is the String "8"?
(!= shorthanded for .equals)
8 != r
8 != R
8 != c
8 != C
p1area p2area have not been initialized and can not be used.
Re: Very basic help needed
but i did create the variables at the beginning of main() and in the if statements i called functions and assigned the returned value to the variables..but as soon as i try to use those variables outside of the if statements i get an error
Re: Very basic help needed
You declared the variables.
The complaint is that they were not initialized. (A fancy way to say "given a value")
Every possible path through your code that leads up to the use of any variable (p1area) must include a line of code that gives a value to the variable. (somewhere through the path has to say p1area = 24 or some valid value)
As it stands if a bad value, like the String "8" or the String "fred", was returned as the value for the showInputDialog call, p1area and p2area will not be given a value.
Your code has no way to know if showInputDialog will return one of r, R, c, C or not, so the compiler can see there is a way to read a variable you have not given a value yet.
Re: Very basic help needed
so what would be a good alternative to how I'm proceeding through this?
Re: Very basic help needed
You have to make sure that every possible path through the code will initialize the variables before you use them.
When and where you initialize them depends on what you need. The value you give depends also on what you need.
I would start early, like where I declare my variables, I usually try to initialize them with a value.
Can you visualize different possible paths through the code? If (condition) this else that... That makes two possible paths to go down. You just have to make sure the variables that are used were forcefully set to a value before you try to read the value, no matter which path was taken.