# Square root not working

• January 7th, 2011, 04:41 PM
sp11k3t3ht3rd
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.

Code :

```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:

Code :

```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 . . .```
• January 7th, 2011, 05:24 PM
sp11k3t3ht3rd
Re: Square root not working
I figured it out. I needed you put Math.sqrt. Sorry about that! :P
• January 7th, 2011, 05:25 PM
helloworld922
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.

Code Java:

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

Code Java:

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