• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Division Operation

 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at this code, you need either:

(note the lower-case 'd')
or

[ April 16, 2003: Message edited by: Joel McNary ]
 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
 
smitha rai
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Smitha, before you post again, please change your displayed name according to the Java Ranch Naming Policy, Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic