Results 1 to 7 of 7
  1. #1
    jmanv888 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default Math.pow returning cannot find symbol

    I am very new to java and I don't know if I am not using the proper input or what. I get:

    cannot find symbol
    symbol: method pow(double,double)
    location: class Math

    I know I am doing something small wrong but I don't know what it is. Please help

    Thank you,
    Jason

    Java Code:
    import java.util.Scanner;
    
    public class Math {
        public static void main(String[] args) {
    
    	Scanner kbReader = new Scanner(System.in);    
     
    	System.out.println("Make your arithmetic selection from the choices below:\n");
    	System.out.println(" A. Addition");
    	System.out.println(" S. Subtraction");
    	System.out.println(" M. Multiplication");
    	System.out.println(" D. Division");
    	System.out.println(" P. Power\n");
    	
    	String choice = kbReader.nextLine();
    
    	char ch = choice.charAt(0); 
    	
    	System.out.print("\nEnter First operand.");
    	double op1 = kbReader.nextDouble();
    
    	System.out.print("\nEnter second operand.");
    	double op2 = kbReader.nextDouble();
    
    	System.out.println(" ");
    	switch (ch)
    
    	{
    	case 'A': //addition
    	case 'a': //Notice we are providing for both capital A and little a.
    	System.out.println(op1 + " plus " + op2 + " = " + (op1 + op2));
    	break;
    
    	case 'S': //subtraction
    	case 's':
    	System.out.println(op1 + "| minus " + op2 + " = " + (op1 - op2));
    	break;
    
    	case 'M': //multiplication
    	case 'm':
    	System.out.println(op1 + " times " + op2 + " = " +(op1 * op2));
    	break;
    
    	case 'D': //division
    	case 'd':
    	System.out.println(op1 + " divided by " + op2 + " = " + (op1 / op2));
    	break;
    
    	case 'P': //Power
    	case 'p':
    	System.out.println(Math.[B]pow[/B](op1, op2));
    	break;
    
    	default:
    	System.out.println("Hey dummy, enter only a A, S, M, D, or P!");
    	}
    
       }
    }
    Last edited by jmanv888; 07-09-2011 at 08:16 PM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Use [code][/code] tags instead of quote tags when posting code. It keeps the formatting.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    jmanv888 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    Sorry, tags were fixed.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Never name a class of your own the same as a class in the java.lang package. And don't import packages or classes that aren't used. That just makes the code confusing for the reader.

    Your class Math doesn't have a method pow(double, double) so why does the error message surprise you?

    db

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    You are trying to use a method from the library class called java.lang.Math, but you've called your own class Math, which hides or shadows the java.lang.Math class - so the compiler is looking for that method in your class.

    One solution when you get a clash of names is to explicitly qualify the full path of the method or class you want to use. e.g. System.out.println(java.lang.Math.pow(op1, op2)); but a sensible coder would not give his class the name of a common library class, especially if he wanted to use that library class in his own!

    Good naming choice is one of the most neglected parts of programming.

  6. #6
    jmanv888 is offline Member
    Join Date
    Jul 2011
    Posts
    4
    Rep Power
    0

    Default

    You guys are awesome! I knew I was doing something stupid. I didn't even realized I did that. Thank you very much! I really appreciate it a lot! I will have to keep naming in the back of my head now so I dont do anything like that again.

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Good names describe what the method or class does, so ideally you can read and understand a program from the names alone. For classes use noun forms, for methods use verb forms.
    Last edited by dlorde; 07-10-2011 at 03:02 AM. Reason: oops! don't know my adjectives from my verbs...

Similar Threads

  1. Cannot find Symbol
    By webbby in forum New To Java
    Replies: 3
    Last Post: 04-14-2011, 01:51 AM
  2. Cannot find symbol
    By Johanis in forum New To Java
    Replies: 18
    Last Post: 11-09-2010, 09:34 PM
  3. Replies: 3
    Last Post: 12-10-2008, 02:38 AM
  4. Replies: 9
    Last Post: 10-18-2008, 09:26 PM
  5. Replies: 3
    Last Post: 05-01-2008, 09: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
  •