格式转换的麻烦得到奇怪的错误

时间:2016-02-23 20:49:25

标签: java arrays compiler-errors

所以我一直在研究这段代码,并且修复了很多错误但是现在当用户输入数字作为payrates它给我一些奇怪的错误时,首先发布代码而不是发布错误< / p>

import java.util.Scanner;
import java.util.*;
public class project3
{
   private static double[] payrate;
   private static String[] names;


   public static void SortData(double payrate[])
    {
         int first;
         int temp;
         int i;
         int j;
         for(i = payrate.length - 1; i > 0; i--)
         {
            first = 0;
            for(j = 1; j<=i;j++)
            {
               if(payrate[j]<payrate[first])
               first = j;
            }
            temp = (int)payrate[first];
            payrate[first] = payrate[i];
            payrate[i] = temp; 
         } 
    }

   public static void GetData()
   {

    Scanner input = new Scanner(System.in);
     System.out.println("How many names do you want to enter?");
                String strNum = input.nextLine();
                int num = Integer.parseInt(strNum);
                int array[] = new int[num];
                for (int i = 0  ; i < array.length ; i++ ) 
                    {
                        names = new String[num];


                        System.out.println("enter employee's name: ");

                        names[i] = input.nextLine(); 
                        //while(names[i].length < 2)
                        //{
                           //System.out.println("enter valid employee's name: ");

                        //names[i] = input.nextLine(); 
                        //} 

                    }
                for(int j = 0; j < array.length;j++)
                    {
                        payrate = new double[num];
                        System.out.println("enter employee's payrate: ");
                        payrate[j] = input.nextDouble();
                        while(payrate[j] > 100 || payrate[j] < 0)
                        {
                           System.out.println("enter valid employee's payrate: ");
                           payrate[j] = input.nextDouble();
                        }


                     }


   }
        public static void DisplayData(double payrate[], String names[])
    {

         System.out.printf("Name    PayRate\n");
         for (int l=0; l< names.length; l++) 
         {

           //for(int i=0;i<names.length;i++)
          // {
              System.out.print(names[l]);

           System.out.printf("%6d\n", payrate[l]);

           //} 
      }
    }

    public static void main(String[] args)
   {
      GetData();
      SortData(payrate);
      DisplayData(payrate,names);

   }

它给我的错误是我认为的DisplayData方法,这是错误:

nullException in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
    at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
    at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
    at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
    at java.util.Formatter.format(Formatter.java:2520)
    at java.io.PrintStream.format(PrintStream.java:970)
    at java.io.PrintStream.printf(PrintStream.java:871)
    at project3.DisplayData(project3.java:80)
    at project3.main(project3.java:90)

1 个答案:

答案 0 :(得分:-1)

您使用的格式字符串不正确。例如,请参阅此相关问题:Java: Double % formatting question for printf