• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

error: cannot find symbol

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK I am getting can't find symbol and i checked the spelling of varbiles but it seems my 2nd method is not seeing the variables and i can not figure this out. I am new to java coding and lost. I looked at the java website but nit sure where to look for the answer.

please help if you can.


errors:
----jGRASP exec: javac -g Interest.java

Interest.java:29: error: cannot find symbol
earned = salary * rate;
^
symbol: variable earned
location: class Interest
Interest.java:29: error: cannot find symbol
earned = salary * rate;
^
symbol: variable salary
location: class Interest
Interest.java:29: error: cannot find symbol
earned = salary * rate;
^
symbol: variable rate
location: class Interest
Interest.java:30: error: incompatible types: missing return value
return;
^
4 errors


thank you in advance
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Local variables are only in the scope of the block for which it is declared.... so, the local variables declared in the main() method, are not scope for the calculateInterest() method.

Henry
 
Siens Alot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that what i thought but if i move the varbiles before the 1st method to make them available to both classes I get the below error. How would i write this so both classes can use the same variables?

----jGRASP exec: javac -g Interest.java

Interest.java:20: error: non-static variable salary cannot be referenced from a static context
salary = input.nextDouble();
^
Interest.java:31: error: non-static variable earned cannot be referenced from a static context
earned = salary * rate;
^
Interest.java:31: error: non-static variable salary cannot be referenced from a static context
earned = salary * rate;
^
Interest.java:31: error: non-static variable rate cannot be referenced from a static context
earned = salary * rate;
^
Interest.java:32: error: incompatible types: missing return value
return;
^
Interest.java:34: error: non-static variable salary cannot be referenced from a static context
System.out.println("Interest earned on $" + salary + " at 5% would be $"+ earned );
^
Interest.java:34: error: non-static variable earned cannot be referenced from a static context
System.out.println("Interest earned on $" + salary + " at 5% would be $"+ earned );
^
7 errors

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
 
Greenhorn
Posts: 9
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Siens,
A lot of changes need to be made in your code, as it is missing a lot of code.
The code is as follows:
import java.util.Scanner;

public class Interest
{
static double salary;
static double rate= 0.05;
static double earned;
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("What is the original amount?");
salary = input.nextDouble();

calculateInterest();

System.out.println("Interest earned on $" + salary + " at 5% would be $"+ calculateInterest() );

}

public static double calculateInterest()
{

earned = salary * rate;
return earned;
}
}


As far as i know this is proper code, let me know if any query.
Corrections are welcome as i am also new to programming.

Thanks.
 
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I suggest you start by removing the keyword static from everywhere you don't need it. I think the only place you really need it is in the heading for the main method, which mist be static. If the interest rate is always going to be 5% however, then there is an argument for making rate a constant, and constants are often static.
You now have the opportunity to create an Interest object for each amount. So you want a constructor, and you can call it like this:-
Interest i = new Interest(1000000);
I think you can probably find a much better identifier than i, but don't try int as an identifier. Those values should be fields of the class. Why are you using salary as a name? You don't get interest on a salary but on a principal. The principal is what you call the original deposit, or the original amount borrowed.
Make sure you give your Interest class a toString method, then you can call it simply like this:-
System.out.println(i); // Using my bad identifier
 
himashree rawat
Greenhorn
Posts: 9
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for such a detail explanation Campbell, its quiet valuable.
 
Campbell Ritchie
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Siens Alot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the help and suggestions, question tho if i removed all the static from everything except the main method then i reeive the error below?
Interest.java:20: error: non-static variable salary cannot be referenced from a static context


how do i get the 2nd method to pull the variables if not static as I am missing something and they will not see each other?

I am greener then green and appreciate the help and suggestions.

Siens
 
Campbell Ritchie
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are not creating objects.That is an ideal length for a main method: one statement.

Obviously you need a MyApplication class with a (non‑static) start method.
 
Siens Alot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So per your example I just call the other method from the main method. which i tried to do and got the non static error message. Also i am not sure about the .start(); after the call to the 2nd method?

 
Campbell Ritchie
Marshal
Posts: 80619
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to create a method called start or something like that to start your application. That will obviously call other methods, create other objects, etc.Now all you need is an Interest class. With the possible exception of rate if that is a constant, nothing in that class should be static.
 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Siens Alot wrote:that what i thought but if i move the varbiles before the 1st method to make them available to both classes I get the below error. How would i write this so both classes can use the same variables?



Nope, the answer is correct -- meaning it is a compiler error because the local variables are out of scope. This error is something else -- you took broken code, and broke it differently.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic