This week's book giveaway is in the Kotlin forum.We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Calculating with Java

Shirl Diva
Greenhorn
Posts: 21
Is there a problem, in java, when it comes to calculating arithmetic problems that are more than two pronged. Why am I having so much trouble trying to multiply 2 * radius * diameter??

Ron Newman
Ranch Hand
Posts: 1056
I don't know. What trouble are you having? What are the types of those variables?

Cain Silverbane
Greenhorn
Posts: 13
hrmm... shouldnt have trouble, I wouldn't think... what exactly are you doing?
doing things such as x * y * z should be just as easy as x * y. Be sure to remember to have it all stored into something:
volume = length * height * width;

Shirl Diva
Greenhorn
Posts: 21
I was supposed to write a tiny program that prompted the user for a value, for the radius of a circle. The pgm then calcs the diameter, area, and circumference of the circle from that value. I had all of my variables in place. I tried to do something simple like circ = radius * pi * diameter. It would not take it. The error msg went something like this...I can't remember exactly. I can't find my notes either. It said something about me using 'double' int. I looked that up and used the (double) with it and it still wouold not work. I'm sorry guys but I was putting another query up - that's why I was not ready with this one. I am just loaded with problems

Shirl Diva
Greenhorn
Posts: 21
I found it!! The error msg is "possible loss of precisiion: double required int." It calculated radius = diameter * 2. When it came to the "Circumference = 2 * Math.PI * Radius - it was having no part of it.

Shirl Diva
Greenhorn
Posts: 21
Originally posted by Cain Silverbane:
hrmm... shouldnt have trouble, I wouldn't think... what exactly are you doing?
doing things such as x * y * z should be just as easy as x * y. Be sure to remember to have it all stored into something:
volume = length * height * width;

The variables have been changed from string to integer and the code goes like this "Circumference = 2 * Math.PI * Radius;" The msg that I get is "possible loss of precision; double required int", I went back in and used the (double) and it still didn't accept it.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
Try:
Circumference = 2.0d * Math.PI * Radius;
And make sure Circumference and Radius are both doubles.

Ron Newman
Ranch Hand
Posts: 1056
Math.PI is a double, so the result of your multiplication will be a double. You must store it in a double variable, not an int.

Dirk Schreckmann
Sheriff
Posts: 7023
Shirl,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!