• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Simple program question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!

 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look into, NumberFormat/DeciamlFormat classes
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic