• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Illegal format conversion exception for printf

 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get this printf to work for displaying double variables that have been cast typed to int. I am not really sure what to do here.




Here is the error report I got when compiling the code:



First Second Third Fourth Fith Sixth
----- ------ ----- ------ ---- -----
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 GreedGame.main(GreedGame.java:152)


 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12100
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is line 152?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that, line 152 is:



would i need to set DATA_FMT_STR to an int or double for it work?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What form are the variables to be printed? You would appear to be passing doubles tothe %d tag which expects an integer. Is min an int or a double?
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for pointing that out, rather obvious I must admit looking at it. So I am no longer getting an error, but now I am getting a floating point result, as to be expected. How could I convert the double variables to produce an int result for the printf method?

This is what I am getting now.



Please roll your die ("roll" or "end_turn"): roll
First Second Third Fourth Fith Sixth
----- ------ ----- ------ ---- -----
4.000000 2.000000 3.000000 4.000000 1.000000 4.000000



Thanks a lot for your help
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want integer result from a die, so you want its variables in integer format. Declare min as an int.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well min and max are declared as ints already. I believe the problem lies in the fact that dieOne through dieSix are double and need to be double bc:



require a double variable (atleast that what my book tells me with regards to Math.random() methods, that and I stoped getting errors when I declared them doubles then cast typed them to int.

As before I tried using the %d formatter which I found didnt work because it found double variables not int. So now I am wondering if I should create int variables to assign the value from the double variables to it and result an int value to be printed. Does that sound like a good idea?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, dieOne, dieTwo, etc should be ints because the results of dice are always whole numbers.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I presume you haven't leanred about arrays yet.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I went and did a cast type to convert from double to int, compiler didnt complain about that.



These are the changes I have made. The errors I am getting now are the ones below. To note I did change:


from %10f to %10d for integers



GreedGame.java:160: ')' expected
System.out.printf(DATA_FMT_STR, dieOneIn
t dieTwoInt dieThreeInt dieFourInt dieFiveInt dieSixInt);
^ (carrot should be just under begin of dieTwoInt)

GreedGame.java:160: ';' expected
System.out.printf(DATA_FMT_STR, dieOneIn
t dieTwoInt dieThreeInt dieFourInt dieFiveInt dieSixInt);
^ (carrot should be just under begin of dieFiveInt)
GreedGame.java:160: ';' expected
System.out.printf(DATA_FMT_STR, dieOneIn
t dieTwoInt dieThreeInt dieFourInt dieFiveInt dieSixInt);
^ (carrot should just under last ')' )
3 errors





I appreciate any help in the matter
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure, but here



Are you supposed to seperate each of those with a comma? This is the only code that wasn't like dieOneInt, dieTwoInt, dieThreeInt, etc.
 
Ben Hultin
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for all your help, the commas did seem to be the problem, I added the commas in the printf and removed the commas in the DATA_FMT_STR. Now it is working properly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic