Forums Register Login

Simple program question

+Pie Number of slices to send: Send
Below is some very simple code for finding the diameter, circumference, and area of a circle with a radius inputed by the user. Can someone please tell me why it is not running?

Thanks!



[ October 17, 2006: Message edited by: Chris Reech ]

[ October 17, 2006: Message edited by: Chris Reech ]
[ October 17, 2006: Message edited by: Chris Reech ]
+Pie Number of slices to send: Send
 

Originally posted by Chris Reech:
radius = in.readLine(); // Get radius



Replace above code with following code

radius = Double.parseDouble( in.readLine()); // Get radius

Beucase the in.readLine() return String, while radius is a double.

Aslo you need to add try/catch block surrounding your code.
[ October 17, 2006: Message edited by: Chetan Parekh ]
+Pie Number of slices to send: Send
One way is to replace your radius= line with:

radius = Double.parseDouble(in.readLine());

Also, since you're changing a String to Double here, you have to 'try-catch' or 'throws'.

praseDouble method should be used sparingly.
+Pie Number of slices to send: Send
 

Originally posted by Chetan Parekh:


Replace above code with following code

radius = Double.parseDouble( in.readLine()); // Get radius

Beucase the in.readLine() return String, while radius is a double.

Aslo you need to add try/catch block surrounding your code.

[ October 17, 2006: Message edited by: Chetan Parekh ]



Now I got this message
+Pie Number of slices to send: Send
Nothing to do with the problem, but it might be interesting to know that you can set Pi as follows:

final double PI = Math.PI;

 
+Pie Number of slices to send: Send
Chris Reech, Add try/catch block in your code - as I mentioned before.

Check how to do that below

[ October 18, 2006: Message edited by: Chetan Parekh ]
+Pie Number of slices to send: Send
 

Originally posted by Chetan Parekh:
Chris Reech, Add try/catch block in your code - as I mentioned before.

Check how to do that below


[ October 18, 2006: Message edited by: Chetan Parekh ]



Excellent, it worked! 2 more things:

I am very novice so please bear with me. How do you assign the radius to a float variable? Also, I need to drop .0 from the diameter value and shorten the decimal places to 3. Shown below.

Thanks!

+Pie Number of slices to send: Send
look into, NumberFormat/DeciamlFormat classes
+Pie Number of slices to send: Send
Or use System.out.printf(...) instead of System.out.println(...) to print the output. With printf, you can specify the format that you want for the numbers. Look it up in the API documentation (you can find printf(...) in the class java.io.PrintStream).

Example:
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1086 times.
Similar Threads
Circle program errors
Programing challenge
Who should verify the sanity of data?
Help with Circle Class :)
the first code on interface
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:51:12.