printf中的IllegalFormatConversionException

时间:2013-10-13 17:14:22

标签: java printf

我需要一些帮助。我已完成并设置了整个程序。我在printf语句中遇到问题,因为每次运行程序时,即使程序编译完美,我也会收到IllegalFormatConversionException错误。

这是有问题的printf

System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg); 

代码:

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class Hurricanes2
{
    public static void main(String[] args) throws IOException
    {
        int arrayLength = 59;
        int [] year = new int[arrayLength];
        String [] month = new String[arrayLength];
        int [] pressure = new int[arrayLength];
        int [] speed = new int[arrayLength];
        String [] name = new String[arrayLength];
        double [] speedMPH = new double[arrayLength];
        int [] category = new int[arrayLength];
        int c1 = 0;
        int c2 = 0;
        int c3 = 0;
        int c4 = 0;
        int c5 = 0;
        int categoryMin = 0;
        int categoryMax = 0;
        int speedMin = 0;
        int speedMax = 0;
        int pressureMin = 0;
        int pressureMax = 0;
        int categorySum = 0;
        int speedMPHSum = 0;
        int pressureSum = 0;
        double categoryAvg = 0;
        double speedMPHAvg = 0;
        double pressureAvg = 0;

        File fileName = new File("hurcdata2.txt");
        Scanner inFile = new Scanner(fileName);

        int index = 0;
        while (inFile.hasNext())
        {
            year[index] = inFile.nextInt();
            month[index] = inFile.next();
            pressure[index] = inFile.nextInt();
            speed[index] = inFile.nextInt();
            name[index] = inFile.next();
            index++;
        }

        inFile.close();

        for(index = 0; index < arrayLength; index++)
        {
           speedMPH [index] = (speed[index]) * 1.15;
            if(speedMPH[index] >= 74 && speedMPH[index] <= 95)
           {
                category[index] = 1;
            c1++;
           }
            else if(speedMPH[index] >= 96 && speedMPH[index] <= 110)
           {
                category[index] = 2;
            c2++;
           }
           else if(speedMPH[index] >= 111 && speedMPH[index] <= 130)
           {
                category[index] = 3;
            c3++;
           }
                else if(speedMPH[index] >= 131 && speedMPH[index] <= 155)
           {
                category[index] = 4;
            c4++;
           }
           else
           {
                category[index] = 5;
            c5++;
           }
        }

        for(index = 0; index < arrayLength; index++)
        {
            categoryMin = category[index];
            if(category[index] < categoryMin)
            {
                category[index] = categoryMin;
            }

            categoryMax = category[index];
            if(category[index] > categoryMax)
            {
                category[index] = categoryMax;
            }

            speedMin = speed[index];
            if(speed[index] < speedMin)
            {
                speed[index] = speedMin;
            }

            speedMax = speed[index];
            if(speed[index] > speedMax)
            {
                speed[index] = speedMax;
            }

            pressureMin = pressure[index];
            if(pressure[index] < pressureMin)
            {
                pressure[index] = pressureMin;
            }

            pressureMax = pressure[index];
            if(pressure[index] > pressureMax)
            {
                pressure[index] = pressureMax;
            }

            categorySum += category[index];
            speedMPHSum += speedMPH[index];
            pressureSum += pressure[index];
        }
        categoryAvg = (double)categorySum / arrayLength;
        speedMPHAvg = (double)speedMPHSum / arrayLength;
        pressureAvg = (double)pressureSum / arrayLength;

        System.out.printf("%40s", "Hurricanes 1980 - 2006");
        System.out.println();
        System.out.println();
        System.out.printf("%s%10s%10s%10s%10s", "Year", "Hurricane", "Category", "Pressure (mb)", "Wind Speed (mph)");
        System.out.println();
        System.out.println("=====================================================================");
        for(index = 0; index < arrayLength; index++)
        {
            System.out.printf("%d%10s%10d%10d%10f", year[index], name[index], category[index], pressure[index], speedMPH[index]);
            System.out.println();
        }
    System.out.println("=====================================================================");
    System.out.println();

    System.out.printf("%s%20d%10d%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);
    System.out.println();
    System.out.printf("%s%20d%10d%10.1f", "Maximum", categoryMax, pressureMax, speedMax);
    System.out.println();
    System.out.printf("%s%20d%10d%10.1f", "Minimum", categoryMin, pressureMin, speedMin);
    System.out.println();

    System.out.println("Number of Category 1 Hurricanes: " + c1);
    System.out.println("Number of Category 2 Hurricanes: " + c2);
    System.out.println("Number of Category 3 Hurricanes: " + c3);
    System.out.println("Number of Category 4 Hurricanes: " + c4);
    System.out.println("Number of Category 5 Hurricanes: " + c5);


    }//end main()
}//end Hurricanes2

1 个答案:

答案 0 :(得分:0)

printfd is for integral types only, i.e. byte, short, int and long,而不是double

由于categoryAvgpressureAvgdouble,因此请改为使用f

System.out.printf("%s%20f%10f%10.1f", "Average", categoryAvg, pressureAvg, speedMPHAvg);

同样f不适用于int

因此,由于speedMaxspeedMinint,因此您应该使用d代替:

System.out.printf("%s%20d%10d%10d", "Maximum", categoryMax, pressureMax, speedMax);
System.out.printf("%s%20d%10d%10d", "Minimum", categoryMin, pressureMin, speedMin);

(或适当更改变量的类型)。