Results 1 to 20 of 28
Thread: Can't find symbol
- 09-09-2012, 04:55 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Can't find symbol
I am writing a basic program to find the roots of a quadratic equation using the quadratic formula. However, I am getting the error in the title 11 times, can somebody please explain why?
Java Code:class Quadratic { public static void main(String[] args) { calculation(1, 1, -6); System.out.println("The quadritic equation entered was:"); System.out.print(aValue + "x" + "\u00B2"); System.out.print(" + "); System.out.print(bValue + "x"); System.out.print(" + "); System.out.println(cValue); System.out.println("The roots are:"); System.out.println(plusAnswer + " and " + minusAnswer); } public static void calculation(int a, int b, int c) { aValue = a; bValue = b; cValue = c; if (aValue <= 0) { System.out.println("Only enter a positive number for a"); return; } else { double plusAnswer = ((b*(-1))+sqrt(pow(b,2)-(4*a*c)))/(2*a); double minusAnswer = ((b*(-1))-(sqrt(pow(b,2)-(4*a*c))))/(2*a); } } }
- 09-09-2012, 05:02 PM #2
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
Sorted 4 of the errors, forgot the "int" before aValue...
Java Code:public static void calculation(int a, int b, int c) { int aValue = a; int bValue = b; int cValue = c;
- 09-09-2012, 05:19 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
I still have 7 of the errors though
- 09-09-2012, 05:40 PM #4
Re: Can't find symbol
Please post the full text of the error messages that show the lines where the errors are happening.
If you don't understand my response, don't ignore it, ask a question.
- 09-09-2012, 05:40 PM #5
Re: Can't find symbol
There are no variables called aValue, bValue, cValue, plusAnswer and minusAnswer in your main method.
Also there is no method called pow() and sqrt() in your class. I am guessing you are trying to use the Math class for those methods.
- 09-09-2012, 05:58 PM #6
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
do all variables have to be stated in the main method then?
can't I state them in the calculation method?
I have int before the aValue, bValue and cValue though, isn't that declaring them (and double before the "answer" variables)
Do I need Math.*** for the calc methods then?
- 09-09-2012, 06:04 PM #7
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
There are more errors now, it can't find the variables...
Java Code:class Quadratic { public static void main(String[] args) { int aValue, bValue, cValue; double plusAnswer, minusAnswer; calculation(1, 1, -6); System.out.println("The quadritic equation entered was:"); System.out.print(aValue + "x" + "\u00B2"); System.out.print(" + "); System.out.print(bValue + "x"); System.out.print(" + "); System.out.println(cValue); System.out.println("The roots are:"); System.out.println(plusAnswer + " and " + minusAnswer); } public static void calculation(int a, int b, int c) { aValue = a; bValue = b; cValue = c; if (aValue <= 0) { System.out.println("Only enter a positive number for a"); return; } else { plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a); minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a); } } }
- 09-09-2012, 06:05 PM #8
Re: Can't find symbol
You need to define the variables where they are needed. Some are needed in several methods and those should be defined at the class level. Some are only used in a single method, those should be defined in the method and not in the class.
To call a class's static methods, code the name of the class dot methodname: Math.round()
Please copy and paste here the full text of the error messages.There are more errorsIf you don't understand my response, don't ignore it, ask a question.
- 09-09-2012, 06:06 PM #9
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
C:\Users\Mike\Desktop\Learning Java\Programs>javac QuadraticSolver.java
QuadraticSolver.java:21: error: cannot find symbol
aValue = a;
^
symbol: variable aValue
location: class Quadratic
QuadraticSolver.java:22: error: cannot find symbol
bValue = b;
^
symbol: variable bValue
location: class Quadratic
QuadraticSolver.java:23: error: cannot find symbol
cValue = c;
^
symbol: variable cValue
location: class Quadratic
QuadraticSolver.java:25: error: cannot find symbol
if (aValue <= 0) {
^
symbol: variable aValue
location: class Quadratic
QuadraticSolver.java:31: error: cannot find symbol
plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
^
symbol: variable plusAnswer
location: class Quadratic
QuadraticSolver.java:32: error: cannot find symbol
minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a);
^
symbol: variable minusAnswer
location: class Quadratic
6 errors
- 09-09-2012, 06:20 PM #10
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
- 09-09-2012, 06:21 PM #11
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
I tried this, it didn't work...
Java Code:class Quadratic { int aValue, bValue, cValue; double plusAnswer, minusAnswer; public static void main(String[] args) { calculation(1, 1, -6); System.out.println("The quadritic equation entered was:"); System.out.print(aValue + "x" + "\u00B2"); System.out.print(" + "); System.out.print(bValue + "x"); System.out.print(" + "); System.out.println(cValue); System.out.println("The roots are:"); System.out.println(plusAnswer + " and " + minusAnswer); } public static void calculation(int a, int b, int c) { aValue = a; bValue = b; cValue = c; if (aValue <= 0) { System.out.println("Only enter a positive number for a"); return; } else { plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a); minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a); } } }
- 09-09-2012, 06:22 PM #12
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
did I do the Math.*** bit right?
- 09-09-2012, 06:23 PM #13
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
I get this with my (bad) adjustment:
C:\Users\Mike\Desktop\Learning Java\Programs>javac QuadraticSolver.java
QuadraticSolver.java:9: error: non-static variable aValue cannot be referenced from a static context
System.out.print(aValue + "x" + "\u00B2");
^
QuadraticSolver.java:11: error: non-static variable bValue cannot be referenced from a static context
System.out.print(bValue + "x");
^
QuadraticSolver.java:13: error: non-static variable cValue cannot be referenced from a static context
System.out.println(cValue);
^
QuadraticSolver.java:16: error: non-static variable plusAnswer cannot be referenced from a static context
System.out.println(plusAnswer + " and " + minusAnswer);
^
QuadraticSolver.java:16: error: non-static variable minusAnswer cannot be referenced from a static context
System.out.println(plusAnswer + " and " + minusAnswer);
^
QuadraticSolver.java:21: error: non-static variable aValue cannot be referenced from a static context
aValue = a;
^
QuadraticSolver.java:22: error: non-static variable bValue cannot be referenced from a static context
bValue = b;
^
QuadraticSolver.java:23: error: non-static variable cValue cannot be referenced from a static context
cValue = c;
^
QuadraticSolver.java:25: error: non-static variable aValue cannot be referenced from a static context
if (aValue <= 0) {
^
QuadraticSolver.java:31: error: non-static variable plusAnswer cannot be referenced from a static context
plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
^
QuadraticSolver.java:32: error: non-static variable minusAnswer cannot be referenced from a static context
minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a);
^
11 errors
- 09-09-2012, 06:25 PM #14
Re: Can't find symbol
Where are the variables: aValue etc defined?
Why do you need them? You can use a,b,c directly in the method. You are using a,b,c here:
plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a);
What did not work?it didn't work...
You need to post the full text of the error messages.
EDIT: I think we posted at the same time. You should try to put it all in one post and not continue to make changes and new posts.If you don't understand my response, don't ignore it, ask a question.
- 09-09-2012, 06:26 PM #15
- 09-09-2012, 06:33 PM #16
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
Because a, b and c are just parameters of the calculation method and therefore I cannot have their values appear in main (I don't think - I'm new to this).
How do I make them static, do I just type static before them? (I am self-teaching and haven't came across this yet).
What does it mean if they're static?
- 09-09-2012, 06:39 PM #17
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
UPDATE:
I tried putting "static" behind both of the variable declaration and I got no errors when compiling, I'm going to test it out now!
Thank you!
- 09-09-2012, 06:42 PM #18
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Can't find symbol
This is my working code:
Java Code:class Quadratic { static int aValue, bValue, cValue; static double plusAnswer, minusAnswer; public static void main(String[] args) { calculation(1, 1, -6); System.out.println("The quadritic equation entered was:"); System.out.print(aValue + "x" + "\u00B2"); System.out.print(" + "); System.out.print(bValue + "x"); System.out.print(" + "); System.out.println(cValue); System.out.println("The roots are:"); System.out.println(plusAnswer + " and " + minusAnswer); } public static void calculation(int a, int b, int c) { aValue = a; bValue = b; cValue = c; if (aValue <= 0) { System.out.println("Only enter a positive number for a"); return; } else { plusAnswer = ((b*(-1))+Math.sqrt(Math.pow(b,2)-(4*a*c)))/(2*a); minusAnswer = ((b*(-1))-(Math.sqrt(Math.pow(b,2)-(4*a*c))))/(2*a); } } }
How do I print a 2 symbol though?
I searched online and it said to type \u00B2 but it just comes up with a filled in rectangle.
- 09-09-2012, 10:58 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
Re: Can't find symbol
Why should 'a' be positive?
kind regards,
JoWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 09-12-2012, 05:04 PM #20
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Similar Threads
-
Cannot find symbol
By dest in forum New To JavaReplies: 3Last Post: 04-03-2012, 05:38 PM -
Cannot find symbol
By Eleeist in forum New To JavaReplies: 5Last Post: 01-22-2012, 08:36 PM -
cannot find symbol
By LimblessQuasar in forum New To JavaReplies: 4Last Post: 06-11-2011, 10:55 PM -
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
cannot find symbol symbol : class Item location: package platypos.services.order
By officialhopsof in forum New To JavaReplies: 3Last Post: 05-01-2008, 08:30 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks