Math.PI comes up with cannot find symbol error

In summary: Maybe it's not using the "java" executable that you're expecting it to?In summary, the problem is that Math.PI is not found on the command line, and this causes two "cannot find symbol" errors.
  • #1
UltimateSomni
62
0
Math.PI comes up with "cannot find symbol" error

class Pi {
public static void main (String[] args) {
float radius=Float.parseFloat(args[0]);
float shortPi=(float) Math.PI;
float circ= shortPi *(radius+radius);
float area= shortPi *(radius*radius);

System.out.print("with Pi commuted from " +Math.PI);
System.out.println(" to " + shortPi + "...");
System.out.println("A circle radius of " + circ + " cm");
System.out.print("has a circumference of " +area+ "sq.cm");

}}

_______________________________________________________
I get two "cannot find symbol" errors, both of which point to the use of Math.PI. Is there something else I need to do? Math.PI is suppose to be constant value worth 3.141592653589793
 
Technology news on Phys.org
  • #2


I don't know which programming language this is, but I suspect you need to put something at the start of your code to tell the compiler how to find the definition of "Math".
 
  • #3


DrGreg: This is java. Math.PI is a part of java.lang, so no declaration is needed.

UltimateSomni: It's been a long time since I did java, but the opening line looks wrong. IIRC, class Pi should be public class Pi.
 
  • #4


public class Pi leads to the same error in the command prompt

It works for the IDE though.
 
Last edited:
  • #5


UltimateSomni said:
public class Pi leads to the same error in the command prompt

It works for the IDE though.

The problem is probably not your code. There's problems in there, but none of them should be causing that problem. Your IDE seems to be doing things correctly. I suspect there's something wrong with your setup on the command line.

What that problem is, I haven't a clue, as I don't have enough information to figure it out. You could try manually setting your classpath or something like that. I would guess it generally can't find the java libraries.

You might take a close look at what your IDE is using as a command line to launch the program.
 

What does the error "cannot find symbol" mean?

The error "cannot find symbol" means that the compiler cannot recognize the symbol or identifier used in the code. In this case, it is likely that the code is trying to use the value of Math.PI, but the identifier "Math" is not recognized.

Why does the error "cannot find symbol" specifically come up with Math.PI?

Math.PI is a commonly used constant in math calculations, so it is a frequently used identifier in code. Therefore, if there is an issue with recognizing identifiers, Math.PI is likely to trigger the "cannot find symbol" error.

How can I fix the "cannot find symbol" error when using Math.PI?

One way to fix this error is to make sure that the code includes the correct import statement for the Math class. For example, for Java, the code should include "import java.lang.Math;" at the top. Additionally, make sure that the spelling and case of the identifier is correct.

Is the "cannot find symbol" error specific to Math.PI or can it happen with other identifiers?

The "cannot find symbol" error can happen with any identifier that is not recognized by the compiler. It is not specific to Math.PI, but it is a common error when using this identifier.

Are there any other potential causes for the "cannot find symbol" error besides incorrect import statements or misspelled identifiers?

Yes, there could be other causes for this error, such as using the identifier in the wrong scope or not declaring it properly. It is also possible that there is a typo or mistake in the code that is causing the compiler to not recognize the identifier. It is important to carefully review the code and check for any other potential issues.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
1
Views
6K
  • Programming and Computer Science
Replies
2
Views
12K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top