Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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);
    		}
    
    		}
    
    
    }

  2. #2
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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;

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    I still have 7 of the errors though

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  5. #5
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default 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.

  6. #6
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by PhQ View Post
    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.

    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?

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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);
    		}
    
    		}
    
    
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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()

    There are more errors
    Please copy and paste here the full text of the error messages.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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

  10. #10
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by Norm View Post
    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.
    I use them two sets of variables in both methods, therefore I need to quote them at a class level?

    How do I do this?
    I have never done this before

  11. #11
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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);
    		}
    
    		}
    
    
    }

  12. #12
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    did I do the Math.*** bit right?

  13. #13
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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);

    it didn't work...
    What did not 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.

  15. #15
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Can't find symbol

    Quote Originally Posted by kkid View Post
    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
    You need to make the variables above the main method static.
    Last edited by PhQ; 09-09-2012 at 06:28 PM.

  16. #16
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by Norm View Post
    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?
    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.
    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?

  17. #17
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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!

  18. #18
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default 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.

  19. #19
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Can't find symbol

    Why should 'a' be positive?

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Can't find symbol

    Quote Originally Posted by JosAH View Post
    Why should 'a' be positive?
    Thanks for pointing this out, I just didn't think it through much, I meant for it to be not zero, I'll change this now.




    Quote Originally Posted by kkid View Post
    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.

Page 1 of 2 12 LastLast

Similar Threads

  1. Cannot find symbol
    By dest in forum New To Java
    Replies: 3
    Last Post: 04-03-2012, 05:38 PM
  2. Cannot find symbol
    By Eleeist in forum New To Java
    Replies: 5
    Last Post: 01-22-2012, 08:36 PM
  3. cannot find symbol
    By LimblessQuasar in forum New To Java
    Replies: 4
    Last Post: 06-11-2011, 10:55 PM
  4. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 08:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •