Results 1 to 5 of 5

Thread: Printf error

  1. #1
    ks1615 is offline Member
    Join Date
    Feb 2009
    Posts
    11
    Rep Power
    0

    Default Printf error

    Ok I'm giving a txt file with data. I have to print the data using arrays and a for loop. The data all has to be set up neatly in columns so I can only use 1 printf statment.
    Here is the code and the error is:
    Illegal Format Conversion Exception:
    null(in java.util.Formatter$formatSpecifier)
    and its being caused by the following code in the pritn statement
    Java Code:
     (....speedmph[x]);
    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class Hurricanes2
    
    {
        public static void main(String[] args) throws IOException
        {    
          int [ ] year = new int[59];
          String [ ] month = new String[59];
          int [ ] pressure = new int[59];
          int [ ] speed = new int[59];
          double [ ] speedmph = new double[59];
          int [ ] category = new int[59];
          String [ ] name = new String[59];
          File fileName = new File("hurcdata2.txt");
          Scanner inFile = new Scanner(fileName);
          System.out.print("Years\t\tHurricance\tCategory\tPressure(mb)\tWind Speed(mph)");
          System.out.println();
          for(int x = 0; x < 59; x++)
          {
              year[x] = inFile.nextInt();
              month[x] = inFile.next();
              pressure[x] = inFile.nextInt();
              speed[x] = inFile.nextInt();
              name[x] = inFile.nextLine();
              speedmph[x] = (int)speed[x] * 1.15077945;
              if((speed[x] <= 82) && (speed[x] >= 64))
              {
                  category[x] = 1;
              }
              else if((speed[x] <= 95) && (speed[x] >= 83))
              {
                  category[x] = 2;
              }
              else if((speed[x] <= 113) && (speed[x] >= 96))
              {
                  category[x] = 3;
              }
              else if((speed[x] <= 135) && (speed[x] >= 114))
              {
                  category[x] = 4;
              }
              else if(speed[x] > 155)
              {
                  category[x] = 5;
              }
              else
              {
              }
              System.out.printf("%-15d%1s%14d%20d%30d\n", year[x], name[x], category[x], pressure[x], speedmph[x]);
          }
          
          
          inFile.close();
          
              
          
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your speedmph is a floating point number, and you can't use a "d" in the conversion expression. Rather, try "f"

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Incidently, one of the first things you should try when encountering a bug that you can't figure out is to simplify your code until you can isolate the error in exclusion of most everything else. Once you do that, the error becomes obvious to you. For instance, I created this class to test your code:
    Java Code:
    class Fubar2
    {
      public static void main(String[] args)
      {
        int year = 2005;
        int pressure = 200;
        String name = "Fubarable";
        double speedmph = 12.230;
        int category = 1;
    
        System.out.printf("%-15d%1s%14d%20d%30d\n", 
            year, name, category, pressure, speedmph);
      }
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    um,... you're welcome,... I think.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

Similar Threads

  1. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  2. printf error with int and double
    By o0n1 in forum New To Java
    Replies: 1
    Last Post: 09-18-2008, 08:58 PM
  3. error 530 error authentication required
    By rgale in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 05-12-2008, 04:28 PM
  4. Demonstration of printf() method
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:37 PM
  5. printf
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •