Spencer
Spencer

Reputation: 22370

Problem with System.out.printf command in Java

I am new to programming so I apologize beforehand. I am running a program and am having a problem with the System.out.printf method in Java. I know everything else in the program is working correctly (tested it). However this does not seem to work.

The error 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 Advisor_Score.main(Advisor_Score.java:17)

My code is:

import java.lang.Math;
public class Advisor_Score {
    public static void main(String[] args){ 
        double l[] = {101,1,1,1};
        double k[] = {102,2,2,4};
        double m[] = {103,5,5,5,5,5,5,5};
        double All_users[][]={l,k,m};
        double sum[]=new double [All_users.length];
        double [] raw_advisor=new double [All_users.length];
        double []advisor_score= new double [All_users.length];
        for (int i=0;i<All_users.length;i++){
                for(int j=1;j<All_users[i].length;j++){
                        sum[i]+=All_users[i][j];
                }
                raw_advisor[i]=((sum[i]-(3*(All_users[i].length-1)))/4);
                advisor_score[i]= 2.5+(2.5*(1-Math.pow(Math.E, -.5*raw_advisor[i])));
                System.out.printf("%d: %d\n", All_users[i][0], advisor_score[i]);
                }       
    }
}

Not really sure why it's not working.

Upvotes: 54

Views: 56647

Answers (2)

Amer Al zibak
Amer Al zibak

Reputation: 1951

if your dataType is bigDecimal then have to use : %g

Upvotes: 1

Michael Mrozek
Michael Mrozek

Reputation: 175355

%d represents an integer; you want to use %f for a double. See the formatting string syntax in the Javadoc

Upvotes: 153

Related Questions