String.Format for rounding, can't locate illegal format conversion source error?
java, string-formatting
Solution
In
String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9)
you are trying to print an `int` with a format for `double`s or `float`s. That causes the `IllegalFormatConversionException: f != java.lang.Integer`. Since the integer division truncates anyway, it isn't very useful to print an integer with two places after the decimal point. Just divide by the floating point number `9.0` instead of the `int` 9 to get a floating point number that you can format with `%.2f`.
In your
String.format("%.2d", (WeatherSpots[K].CatchCount - 32) * 5 / 9)
the format `%.2d` is invalid since it doesn't make sense to print integers with places after the decimal point.
Problem
I'm writing a program that lets the user input 6 temperature readings, and then either - return the highest original values +celcius version - return the original values + conversion into the celsius version. the code where the arrays values are set is here: ``` System.out.print( "Enter Temperature:\t"); //Get the count... Temp = LocalInput.nextInt(); WeatherSpots[K].CatchCount = Temp; ``` the error message that i get is this ``` java.util.IllegalFormatConversionException: f != java.lang.Integer at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source) at java.util.Formatter$FormatSpecifier.printFloat(Unknown Source) at java.util.Formatter$FormatSpecifier.print(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.util.Formatter.format(Unknown Source) at java.lang.String.format(Unknown Source) at p2list.WeeklyReport(p2list.java:102) at p2list.main(p2list.java:33)" ``` i've also found the exact phrase that gives me trouble: ``` String.format("%.2d", (WeatherSpots[K].CatchCount - 32) * 5 / 9)" ``` i know that the error happens when my `"%._"` doesn't have the right specifier, but all of my variables and arrays are in int, so d should be working here's the rest of the code: This is how i set the 1st array: ``` private static WeatherLocation[] WeatherSpots = new WeatherLocation[6];" ``` This is the class that later arrays use ``` public class WeatherLocations extends WeatherLocation { public String LocationID; public Integer CatchCount;" arrays = WeatherSpots.LoccationID/Catchcount" ``` Here's where the `catchcount` array is set with the user input temperatures ``` int K; for(K = 0 ; K < 6 ; K++){ System.out.print( "Enter Temperature:\t"); Temp = LocalInput.nextInt(); WeatherSpots[K].CatchCount = Temp; ``` Here's the method where i try to call on the `WeatherSpots[K].catchcount` values to convert to celcius ``` int K= 0; for(K = 0 ; K < 6 ; K++){ System.out.println( "" + WeatherSpots[K].LocationID +"\t\t" + WeatherSpots[K].CatchCount + "\t\t" + String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9)); ``` What would be causing the error, if my arrays and variables are the proper types for using `string.format`?