• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Java, Ch3 Pool Puzzle problem: int cant be converted to double  RSS feed

 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




====

Triangle.java:29: error: incompatible types: int cannot be converted to Double
area=(height*length)/2;
^
1 error


====Any suggestion please..
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is unlikely to give any error, because int area is a method local variable. It will give error only if remove that int before the variable area, in that case it will refer to instance variable area and that is of type double, or may be somewhere else in the code you are referring to the area instance variable where the result is of type int and you are trying to assign it to area instance variable of type double.
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you meant to use "double" with a lower case 'd'.
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swastik Dey wrote:This code is unlikely to give any error, because int area is a method local variable. It will give error only if remove that int before the variable area, in that case it will refer to instance variable area and that is of type double, or may be somewhere else in the code you are referring to the area instance variable where the result is of type int and you are trying to assign it to area instance variable of type double.



Without using int before area compiler is giving error:Triangle.java:29: error: incompatible types: int cannot be converted to Double
area=(height*length)/2;
^
1 error

Using int there is no compilation error but area =null in output...

so any further suggesstion
 
Nabanita Sarkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:I think you meant to use "double" with a lower case 'd'.


Thanks a lot..."double " worked fine
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Should consider using an int for area because you are always assigning an int to it in setArea().
Consider changing name of setArea() to calcArea() as you are not passing in a value to be used in setting area like a normal setter.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!