-
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 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();
}
}
-
Your speedmph is a floating point number, and you can't use a "d" in the conversion expression. Rather, try "f"
-
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:
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);
}
}
-
um,... you're welcome,... I think.
-
In abstentia, I'll say thanks for ks1615.