Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format for rounding, can't locate illegal format conversion source error?

I'm writing a program that lets the user input 6 temperature readings, and then either

  1. return the highest original values +celcius version
  2. 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?

like image 909
user1476390 Avatar asked Jun 24 '12 21:06

user1476390


People also ask

What is Illegal format conversion exception?

The IllegalFormatConversionException is an unchecked exception in Java that occurs when the argument that corresponds to a format specifier is of an incompatible type. Since the IllegalFormatConversionException is thrown at runtime, it does not need to be declared in the throws clause of a method or constructor.

What is string format in Java?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Syntax: There is two types of string format() method.

Does string format round off?

If the value to be formatted has more than the specified or default number of decimal places, the fractional value is rounded in the result string. If the value to the right of the number of specified decimal places is 5 or greater, the last digit in the result string is rounded away from zero.


1 Answers

In

String.format("%.2f", (WeatherSpots[K].CatchCount - 32) * 5 / 9)

you are trying to print an int with a format for doubles or floats. 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.

like image 155
Daniel Fischer Avatar answered Sep 21 '22 03:09

Daniel Fischer