| Author |
If Else Statements
|
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
Hello friends, I'm slowly learning but I've ran into another problem, I'll just C&P the code as it describes what the program is intended to do.
The error is a "can't find symbol" pointing to tempwidth and templength, the reason I'm confused is that I thought I wouldn't have to declare these variable, are they not local to the method??
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5793
|
|
All variables have to be declared somewhere. If a variable represents part of the state of an object or of the class overall, then it should be declared as a member variable, and its value will survive across method calls. If a variable is only needed as data for a method to operate on, it should be a local variable, declared inside the method or as a parameter.
There is no "undeclared variables are assumed global" rule in Java.
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
I set the temporary width to width
tempwidth=width
within the method, then if the test is true i was to set width=templength
I'm not sure what part I'm missing as I did include
|
 |
Ivan Franko
Ranch Hand
Joined: May 30, 2011
Posts: 44
|
|
don't forget to declare variables!
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
THANKYOU!!
Of course it's something so small!
I'll try to compile the main method now and see if everything works smoothly
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
Now I'm having another "can't find symbol" error here
In the SortPost.java code I have this
I'm not too sure what to do about this one whereas I noticed the error in the last one straight away.
The line "newSortPost.postType(); " is the problem I think, I just want it to apply the method to it, but not return a value (I think..)
Here is the code for postType()
|
 |
Ivan Franko
Ranch Hand
Joined: May 30, 2011
Posts: 44
|
|
Did you fix the same issue in switchThickness method?
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
Oh, yes I did That class compiles properly now, it's just the other one giving me trouble >.<
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
I used this
and now it compiles.. But I have no idea why it works?
Is that declaring the "newSortPost" in some way?
|
 |
Ivan Franko
Ranch Hand
Joined: May 30, 2011
Posts: 44
|
|
why it works?
Because It is ok.
What do you think about idea to read more information about Java Variable Declaration?
For example, here:webpage
I hope, It will be helpful for you.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5793
|
|
Rykurex Peters wrote:I used this
and now it compiles.. But I have no idea why it works?
Is that declaring the "newSortPost" in some way?
This: declares the newSortPost variable. That is, it says, "Let there be a variable with this type and this name." We have to do this for every variable we want to use in Java. If we don't declare a variable, and just try to use it, then Java says "I don't know what that newSortPost thing is."
|
 |
Rykurex Peters
Greenhorn
Joined: Oct 28, 2012
Posts: 21
|
|
|
Thanks a lot guys, and the link will be really helpful =]!
|
 |
 |
|
|
subject: If Else Statements
|
|
|