用于舍入的String.Format,找不到非法格式转换源错误吗? [英] String.Format for rounding, can't locate illegal format conversion source error?

查看:39
本文介绍了用于舍入的String.Format,找不到非法格式转换源错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,允许用户输入6个温度读数,然后输入

  1. 返回最高原始值+摄氏版本
  2. 返回原始值+转换为摄氏版本.

设置数组值的代码在这里:

  System.out.print(输入温度:\ t");//获取计数...Temp = LocalInput.nextInt();WeatherSpots [K] .CatchCount = Temp; 

我得到的错误消息是这个

  java.util.IllegalFormatConversionException:f!= java.lang.Integer在java.util.Formatter $ FormatSpecifier.failConversion(未知来源)在java.util.Formatter $ FormatSpecifier.printFloat(未知来源)在java.util.Formatter $ FormatSpecifier.print(未知来源)在java.util.Formatter.format(未知来源)在java.util.Formatter.format(未知来源)在java.lang.String.format(未知来源)在p2list.WeeklyReport(p2list.java:102)在p2list.main(p2list.java:33) 

我也找到了给我带来麻烦的确切短语:

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

我知道,当我的%._" 没有正确的说明符时会发生错误,但是我所有的变量和数组都位于int中,因此d应该可以工作

这是其余的代码:

这是我设置第一个数组的方式:

 私有静态WeatherLocation [] WeatherSpots = new WeatherLocation [6]; 

这是以后的数组使用的类

 公共类WeatherLocations扩展WeatherLocation {公共字符串LocationID;public Integer CatchCount;"数组= WeatherSpots.LoccationID/Catchcount 

在此处使用用户输入温度设置 catchcount 数组

  int K;for(K = 0; K <6; K ++){System.out.print(输入温度:\ t");Temp = LocalInput.nextInt();WeatherSpots [K] .CatchCount = Temp; 

这是我尝试调用 WeatherSpots [K] .catchcount 值以转换为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)); 

如果我的数组和变量是使用 string.format 的正确类型,那么将导致错误的原因是什么?

解决方案

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

您正在尝试打印格式为 double s或 float s的 int .这会导致 IllegalFormatConversionException:f!= java.lang.Integer .由于整数除法始终会被截断,因此在小数点后两个位置打印一个整数不是很有用.只需除以浮点数 9.0 而不是 int 9即可得到一个浮点数,您可以使用%.2f 对其进行格式化./p>

在您的

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

%.2d 格式无效,因为打印小数点后的整数没有意义.

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?

解决方案

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.

这篇关于用于舍入的String.Format,找不到非法格式转换源错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆