• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable declared as double not working  RSS feed

 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I got error message when I tried to run the above program.  I discovered it has to do with variable X which is of double type.
How do I resolve this issue?
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch 

What is the exact error when you are running your program?
In Java the error message is often very helpful to debug the issue.
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas Chand wrote:Welcome to the ranch 

What is the exact error when you are running your program?
In Java the error message is often very helpful to debug the issue.


a: 3
v: 2
x: 0
t: 10
The object's position is Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at GravityCalculator.main(GravityCalculator.java:36)
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biodun Adeniji wrote:The object's position is Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double


So you can see now it is not because of the variable of type "double".
You have to have a variable of decimal type because there is a "0.5" involved.

The actual issue is with the format specifier as per the exception shows...java.util.IllegalFormatConversionException
"%d" is the format specifier for integers. It cannot be used for "double".

Now can you find the correct format specifier for "double"?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 31, use %f instead of %d, which is for integer values, not floating point.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@OP Welcome to the Ranch!

@Tapas: sorry for giving it away when you were trying to make OP work a little more. Just feeling the spirit of giving, I guess. 
 
Biodun Adeniji
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @ all. The format specifier resolved the issue.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:@Tapas: sorry for giving it away when you were trying to make OP work a little more. Just feeling the spirit of giving, I guess. 

Wishing you a merry christmas and a blessed new year...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!