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

Illegal format conversion exception for printf

Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Joined: Oct 02, 2003
Posts: 11480
    
  16

Which is line 152?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
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

Joined: Aug 17, 2009
Posts: 135
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
You want integer result from a die, so you want its variables in integer format. Declare min as an int.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Joined: Oct 13, 2005
Posts: 39869
    
  28
Yes, dieOne, dieTwo, etc should be ints because the results of dice are always whole numbers.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
And I presume you haven't leanred about arrays yet.
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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

Joined: Feb 10, 2009
Posts: 710
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.


SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Ben Hultin
Ranch Hand

Joined: Aug 17, 2009
Posts: 135
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Illegal format conversion exception for printf