943,746 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 860
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 2nd, 2009
0

Re: Signing In

Expand Post »
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) );
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewToThis is offline Offline
14 posts
since Jul 2009
Jul 2nd, 2009
0

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.

JAVA Syntax (Toggle Plain Text)
  1. System.out.printf("Area is %f\n", (x * x * math.PI) );
Last edited by wildgoose; Jul 2nd, 2009 at 6:44 pm. Reason: error
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewToThis is offline Offline
14 posts
since Jul 2009
Jul 6th, 2009
0

Re: Trouble with area

Please post your code!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 6th, 2009
0

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).
Last edited by BestJewSinceJC; Jul 6th, 2009 at 9:45 pm.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 7th, 2009
0

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).
Here is the last part of the code for the print out. I do not get any errors when I build the program, but when I run it I get the following:

"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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewToThis is offline Offline
14 posts
since Jul 2009
Jul 7th, 2009
0

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.
Last edited by wildgoose; Jul 7th, 2009 at 1:58 am. Reason: addendum
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 7th, 2009
0

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.
Last edited by JamesCherrill; Jul 7th, 2009 at 1:28 pm.
Featured Poster
Reputation Points: 1907
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Jul 7th, 2009
0

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.
Oh Yeah! That worked. I was assigning a floating point value to all of the equations. Problem solved, you guys are fabulous. I want to excel at this stuff, so I have to ask why is there a difference in Area, Circumference, and Diameter in being a floating point value and an integer value.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
NewToThis is offline Offline
14 posts
since Jul 2009
Jul 7th, 2009
0

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.
Featured Poster
Reputation Points: 1907
Solved Threads: 951
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How to get or calculate coordinates?
Next Thread in Java Forum Timeline: What is the function of "-d ." command?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC