![]() |
| ||
| Re: Signing In 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) ); |
| ||
| Re: Trouble with area 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. System.out.printf("Area is %f\n", (x * x * math.PI) ); |
| ||
| Re: Trouble with area Thanks, that really cleared up my format for the Area and Circumference, but I am still getting an error for diameter. Honestly, I do not know why. I have eliminated one problem by capitalizing the "M" in Math.PI, but can't get over the diameter issue. |
| ||
| Re: Trouble with area Please post your code! |
| ||
| Re: Trouble with area 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). |
| ||
| Re: Trouble with area Quote:
"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 |
| ||
| Re: Trouble with area 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. |
| ||
| Re: Trouble with area 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. |
| ||
| Re: Trouble with area Quote:
|
| ||
| Re: Trouble with area x is an int. When you do a calc with just integers, the result is an int, but if it's mixed ints and floats, the result is float. Pi is a float. Area and circ. involved Pi, bit diameter didn't. |
| All times are GMT -4. The time now is 12:51 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC