Results 1 to 5 of 5
  1. #1
    McChill is offline Member
    Join Date
    Feb 2009
    Posts
    47
    Rep Power
    0

    Default Basic Netbeans question

    I need to put the Math package into this.

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

  2. #2
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    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
    Last edited by MK12; 02-20-2009 at 12:43 AM. Reason: adding
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    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?

  4. #4
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    431
    Rep Power
    6

    Default

    Thanks to MK12.
    Mak
    (Living @ Virtual World)

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Please go to the top of this page, click Thread Tools --> Mark This Thread As Solved.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

Similar Threads

  1. Basic question on OOP.
    By madthinker in forum New To Java
    Replies: 7
    Last Post: 01-26-2009, 07:09 PM
  2. Basic MouseListener Question
    By jshailes in forum AWT / Swing
    Replies: 9
    Last Post: 01-15-2009, 08:58 AM
  3. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  4. Basic different between netbeans and eclipse
    By shu2hua in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 10:34 AM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM

Posting Permissions

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