• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot find symbol, scope of String declaration  RSS feed

 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Howdy, Ranchers.

Now, I am trying to get 'WhatToDo' in my println statement. It repeatedly cannot find symbol. Im not sure if due to whatToDo being declared in if, else statements its possibly out of scope. Countering this I set a value to whatToDo to no avail. Could anyone shed some light on what Is going on here?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It repeatedly cannot find symbol.

If you are getting errors, please copy the full text of the error messages and paste it here.
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PS please ignore the final declaration in public String whatToDo;

Weather.java:57: error: cannot find symbol
System.out.println("What will I do: " + whatToDo (dayOfWeek, goodWeather));
                                        ^
  symbol:   method whatToDo(String,boolean)
  location: class Weather
 
Vijitha Kumara
Bartender
Posts: 4002
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at your System.out.. statement and see what you are trying to do you will understand what's the problem? Check how you have defined "whatToDo" in your class and how you try to use it.

And welcome to CodeRanch!

 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Weather.java:57: error: cannot find symbol
System.out.println("What will I do: " + whatToDo (dayOfWeek, goodWeather));
                                        ^
  symbol:   method whatToDo(String,boolean)

The compiler can not find a method named: whatToDo.  Where is that method defined?  I can't see it in the posted code.

I see Strings with that name but no method.

Make sure there is a definition for the method before trying to call it.
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for being daft. I am unsure what you mean by defining whatToDo as a method.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the tutorial about methods:
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My edit of the code incorporating methods.
     



 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please wrap all posted code in code tags.  Use the Code button above the input area.

Did you have a question about the code you just posted?

If there are error messages, copy and paste here the full text.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your use of parenthesis is sometimes overused and sometimes underused and sometimes not used correctly.
This is a case of overuse
The parens are not necessary and don't improve the readability.

This is a case of underuse
I'm sure you meant
but without the additional parens the operator '&&'  take precedence over the '||' and you'll end up going on a picnic if it is Saturday, regardless of the weather.

Here's a case of improper use
Methods are required to have parens follow the method name. Because you have no operator between "whatToDo" and the open paren the compiler interprets "whatToDo" as the name of a method and that dayOfWeek and goodWeather are parameters passed to that method. Unfortunately "whatToDo" is not a method but a String where you are using it and you haven't provided a suitable "whatToDo()" method so the compiler complains.
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou @Carey Brown, legend. I'm making some beginner mistakes here.
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




Thank you everyone for your kind assistance, I am only months into my programming journey. Now this is my edited and improved code. Good news it compiles and runs, just doesn't return any values. Any suggestions to what could be going wrong with the returns?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  just doesn't return any values

Where does the code receive the returned values that you say are missing?
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to clean up the code a bit. I think your lack of proper indentation may be leading to some confusion.

If the code logic permits, it is recommended to have only one return point in a method, i.e. at the end. In your case you have the "whatToDo" String that you are assigning based on logic. Rather than immeadiatly returning the String save your return for the last line in the method. You'd probably want to change this as well: whatToDo = "error".
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case you are not taking advantage of the logic achieved with nested if()'s.

Would have been more correctly wriitten as

 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Thank you so much for your help! I have done as you recommended. It compiles and runs, but still getting a null return.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main() doesn't call whatToDometh() so where are you getting null?
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will simply get no output at all. It runs and compiles but nothing happens.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your main() doesn't call whatToDometh()
 
Josh Brand
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Different situation same problem! Since the method is static, it shouldn't need to be called. Why am I consistently getting no return value or anything.
 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Brand wrote:Different situation same problem! Since the method is static, it shouldn't need to be called. Why am I consistently getting no return value or anything.

This is where you're wrong. Any method must be called in order for it to do anything. In addition, you need to decide what to do with a method's return value, if any.




 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your method
"calc" is used to hold the result temporarily until it is returned. "calc" is a parameter that is local to this method and contains whatever value was passed in when the method was called. Note that it is a local copy. When the method assigns a result to "calc" it does not have any effect on the variable (or constant) that was passed in when the method was called. So, in this case, you should eliminate calc from the parameter list and just declare it as a variable inside the method.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!