| | |
Trouble with area
Thread Solved
![]() |
•
•
Join Date: Jul 2009
Posts: 14
Reputation:
Solved Threads: 0
I have been trying to build a Java program to prompt the user to enter the radius of a circle and the diameter, circumference, and area will be output through a "System.out.printf" statement. I can get the program to prompt for the radius and it will display the diameter, but will not identify the Area or Circumference. I keep getting an error with the Math variable. What could be wrong with this code for Area:
System.out.printf("Area is %d\n", (x * x * math.PI) );
System.out.printf("Area is %d\n", (x * x * math.PI) );
Ahem!
Given only radius gives you all the other data. No need to input.
Circumference = 2 * PI * r
Area = PI * r * r
etc.
I'm also assuming your x is entered as a Floating-Point value.
Given only radius gives you all the other data. No need to input.
Circumference = 2 * PI * r
Area = PI * r * r
etc.
I'm also assuming your x is entered as a Floating-Point value.
JAVA Syntax (Toggle Plain Text)
System.out.printf("Area is %f\n", (x * x * math.PI) );
Last edited by wildgoose; Jul 2nd, 2009 at 6:44 pm. Reason: error
•
•
Join Date: Sep 2008
Posts: 1,526
Reputation:
Solved Threads: 190
And also post what error you are getting. If you are successfully reading in the radius, then 2 * radius should give you the diameter without problems as long as the radius is declared as the correct type (i.e. float or double).
Last edited by BestJewSinceJC; Jul 6th, 2009 at 9:45 pm.
I am so hungry. :(
•
•
Join Date: Jul 2009
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
And also post what error you are getting. If you are successfully reading in the radius, then 2 * radius should give you the diameter without problems as long as the radius is declared as the correct type (i.e. float or double).
"Diameter is Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.Integer"
System.out.print( "declare radius of circle: " ); // prompt for input
x = input.nextInt(); // read first integer
//display results
System.out.printf("Area is %f\n", (x * x * Math.PI) );// output the Area
System.out.printf("Circumference is %f\n", (2 * x * Math.PI) );// output the circumference
System.out.printf("Diameter is %f\n", (2 * x) );// output the diameter using printf
}//end main
}//end class Circle
Post your entire code! Errors can be misleading without seeing everything in context!
For example if x is an integer or a double it can cause a problem with that printf. The fact that it was multiplied with a float (PI) would make the result a float.
For example if x is an integer or a double it can cause a problem with that printf. The fact that it was multiplied with a float (PI) would make the result a float.
Last edited by wildgoose; Jul 7th, 2009 at 1:58 am. Reason: addendum
•
•
Join Date: Apr 2008
Posts: 964
Reputation:
Solved Threads: 143
Because x is assigned from an input.nextInt(), I assume it's declared as int, so:
%f requires a floating point value, but 2*x is an integer value. Change the format to %d when you print the diameter, OR change the calculation to 2.0*x so the result is floating point.
%f requires a floating point value, but 2*x is an integer value. Change the format to %d when you print the diameter, OR change the calculation to 2.0*x so the result is floating point.
Last edited by JamesCherrill; Jul 7th, 2009 at 1:28 pm.
•
•
Join Date: Jul 2009
Posts: 14
Reputation:
Solved Threads: 0
•
•
•
•
Because x is assigned from an input.nextInt(), I assume it's declared as int, so:
%f requires a floating point value, but 2*x is an integer value. Change the format to %d when you print the diameter, OR change the calculation to 2.0*x so the result is floating point.
![]() |
Similar Threads
- Get form to send email (ASP)
- Having trouble understanding how header files work. (C++)
- Bay Area Rental Housing Web Startup (Web Development Job Offers)
- Software engineer for start up in Bay area (Software Development Job Offers)
- area/volume (C)
- trouble understanding widget commands (Python)
- need help with programing with fstream to find area of triangle (C++)
- Connection Problems (Networking Hardware Configuration)
Other Threads in the Java Forum
- Previous Thread: How to get or calculate coordinates?
- Next Thread: What is the function of "-d ." command?
| Thread Tools | Search this Thread |
account android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer homework html ide image inheritance integer integration intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield jtree julia linux loop method midlethttpconnection migrate mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source oracle plazmic print problem program property ria scanner server set sharepoint smart sms smsspam sourcelabs splash sql sqlite subclass support swing testautomation textfield threads tree trolltech unlimited utility windows






