aspose file tools*
The moose likes Beginning Java and the fly likes Division Operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Division Operation" Watch "Division Operation" New topic
Author

Division Operation

smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
Appreciate if some one would rectify the problem in the following code.
Smitha
public class testD
{

public static void main(String[] args)
{
String strA = "10";
String strB = "4";

Double x = Double.valueOf(strA) / Double.valueOf(strB) ;

}
}
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Looking at this code, you need either:

(note the lower-case 'd')
or

[ April 16, 2003: Message edited by: Joel McNary ]

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
Double x = Double.valueOf(strA)/Double.valueOf(strB);
OR
Double x = new Double(Double.valueOf(strA)/Double.valueOf(strB));
Does not work. The compiler generates error like "The operator / is undefined for argument type(s), java.lang.Double, java.lang.Double".
Please explain.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
smitha,
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Originally posted by smitha:
Double x = Double.valueOf(strA)/Double.valueOf(strB);
OR
Double x = new Double(Double.valueOf(strA)/Double.valueOf(strB));
Does not work. The compiler generates error like "The operator / is undefined for argument type(s), java.lang.Double, java.lang.Double".
Please explain.


Doh!! Silly me. I'm not thinking. You cannot divide a Double object by another Double object (this would require the operator overloading supported by C++). Instead, try this:

Double.valueOf returns a Double object, but Double.parseDouble returns a double primitive.
Donald R. Cossitt
buckaroo
Ranch Hand

Joined: Jan 31, 2003
Posts: 401



You forgot to "notice lower-case" as suggested.
Double != double. One is an object Double and the other is primitive double.
Check out this tutorial and the tutorial links embedded.
doco


doco
smitha rai
Ranch Hand

Joined: Aug 15, 2001
Posts: 33
Thanks.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Smitha, before you post again, please change your displayed name according to the Java Ranch Naming Policy, Thanks


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Division Operation