
Basic Netbeans question
I need to put the Math package into this.
Code:
package javaapplication2;
import java.lang.Math;
/**
*
* @author Sterling
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double x = 23.45;
double ans;
ans = sqrt(x);
System.out.println("The square root of 23.45 is " + ans);
}
}
That is what I have so far. It keeps telling me sqrt is not found and java.lang.Math is an unused import. Am I just putting the import in the wrong place? I'm really new to Netbeans also.

1. java.lang.Math is an unecessary import because it is automatically imported into every class, you don't need to import it so get rid of that statement.
2. The error is you have to type Math.sqrt (if sqrt is a static method), not just sqrt. To be able to leave out the Math., you would have to do "import static java.lang.Math;", but this should only be used when there are many references to static Math methods. As stated in the API, use static imports very sparingly, you would need many references to static Math methods to justify using a static import. So the solution, use Math.sqrt(x);, and get rid of the import. Hope this helped.
MK12

Well said MK12, actually this is nothing to do with NetBeans. This is all about Java basis. Seems our thread starter McChill is a newbie to Java. Did you referring any materials about Java?


Please go to the top of this page, click Thread Tools > Mark This Thread As Solved.
MK12