• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple program question  RSS feed

 
Chris Reech
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
art gems
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Reech
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Chris Reech
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
look into, NumberFormat/DeciamlFormat classes
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!