Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Square root not working

  1. #1
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Square root not working

    This is my program that does the distance formula for you. I don't understand why it can't find the method for doing square root unless that changed? My code is here and the error is below it.

    import java.lang.Math;
     
    public class Distance_Formula {
     
    	public static void main(String [] args) {
     
    		userInput input = new userInput();
    		double x1 = 0.0;
    		double x2 = 0.0;
    		double y1 = 0.0;
    		double y2 = 0.0;
    		double distancePreSquareRoot = 0.0;
    		double distancePostSquareRoot = 0.0;
    		String x1prompt = "Enter the X1 value: ";
    		String x2prompt = "Enter the X2 value: ";
    		String y1prompt = "Enter the Y1 value: ";
    		String y2prompt = "Enter the Y2 value: ";
     
    		input.getUserInput(x1prompt);
    		x1 = Double.parseDouble(input.input);
    		input.getUserInput(x2prompt);
    		x2 = Double.parseDouble(input.input);
    		input.getUserInput(y1prompt);
    		y1 = Double.parseDouble(input.input);
    		input.getUserInput(y2prompt);
    		y2 = Double.parseDouble(input.input);
     
    		distancePreSquareRoot = (((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)));
     
    		distancePostSquareRoot = sqrt(distancePreSquareRoot);
     
    		System.out.println("The distance between (" + x1 + "," + y1 + " and (" + x2 + "," + y2 + ") is: " + distancePostSquareRoot);
     
     
    	}
     
    }

    Error:

    Distance_Formula.java:30: cannot find symbol
    symbol  : method sqrt(double)
    location: class Distance_Formula
                    distancePostSquareRoot = sqrt(distancePreSquareRoot);
                                             ^
    1 error
    Press any key to continue . . .
    Last edited by sp11k3t3ht3rd; January 7th, 2011 at 06:14 PM.


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Square root not working

    I figured it out. I needed you put Math.sqrt. Sorry about that!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Square root not working

    In Java there's no such thing as a "global" variable/method. Thus, for methods outside of your current class (including the inheritance hierarchy) you must quantify where that variable/method came from.

    distancePostSquareRoot = Math.sqrt(distancePreSquareRoot); // sqrt is found inside the Math class as a static method

    An alternative available starting in SE 5 is something known as "static imports". This lets you do things such as importing static methods/variables and using them without needing to quantify where they come from. Note that you should be careful when using static imports.

    import static java.lang.Math.sqrt;
     
    public class SomeClass
    {
        public static void main(String[] args)
        {
            double a = sqrt(5);
        }
    }

Similar Threads

  1. JAVA MAGIC SQUARE
    By hiimjoey11 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: November 28th, 2012, 12:42 AM
  2. Latin square logic
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2010, 09:38 AM
  3. Finding square root
    By Tracy22 in forum The Cafe
    Replies: 1
    Last Post: October 18th, 2010, 06:18 PM
  4. Java program Square root
    By Hey in forum Java Theory & Questions
    Replies: 5
    Last Post: August 16th, 2009, 01:14 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM