将String解析为Float值时的NumberFormatException [英] NumberFormatException when parsing String to Float value

查看:197
本文介绍了将String解析为Float值时的NumberFormatException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  System.out.println(Enter a number:); 

String in1 = input.nextLine();

Integer input1 = Integer.valueOf(in1);
Float input2 = Float.parseFloat(in1);
Double input3 = Double.valueOf(in1).doubleValue();

System.out.println();
System.out.println(输入另一个数字:);

String in2 = input.nextLine();
Integer input21 = Integer.valueOf(in2);
Float input22 = Float.parseFloat(in2);
Double input23 = Double.valueOf(in2).doubleValue();

FloatN fco = new FloatN();

System.out.println();
System.out.println(这两个数字的总和为:+ fco.add(input2,input22));
完成= true;

我很清楚这个程序是完全不切实际的,我只写它来练习解析,泛型和接口。我尝试了整数,它工作正常,但尝试Float和Double.add()函数时,我得到3个错误:



at java.lang.NumberFormatException.forInputString



at java.lang.Integer.parseInt



at java.lang.Integer.valueOf



我删除了整数解析器,程序运行良好。我很困惑,为什么只有当我输入十进制值时才会出错,并且希望有人帮助指出究竟是什么导致异常,以便将来避免任何类似的错误,并且由于移除整数解析器会消除此外,如果任何人因为任何原因需要FloatN类:



<$ p $ (
public FloatN(){}
public Float add(float a,float b)
{$ b $ public static float class FloatN implements Summization< Float> b返回a + b;






Summization是一个带有add()方法的通用接口。

在此先感谢。 值作为输入, Integer.parseInt()方法将无法解析它。如果你仍然想在你的代码中使用它们,你必须得到 Float 值的int值。您可以使用 intValue()方法:

  Float input2 = Float。 parseFloat(IN1); 
Integer input1 = Integer.valueOf(input2.intValue());


System.out.println("Enter a number: ");

String in1 = input.nextLine();

Integer input1 = Integer.valueOf(in1);
Float input2 = Float.parseFloat(in1);
Double input3 = Double.valueOf(in1).doubleValue();

System.out.println();
System.out.println("Enter another number: ");

String in2 = input.nextLine();
Integer input21 = Integer.valueOf(in2);
Float input22 = Float.parseFloat(in2);
Double input23 = Double.valueOf(in2).doubleValue();

FloatN fco = new FloatN();

System.out.println();
System.out.println("The sum of both of your numbers is: " + fco.add(input2, input22));
done = true;

I'm well aware that this program is completely impractical, I only wrote it to practice parsing, generics, and interfaces. I tried Integer, which worked fine, but upon trying the Float and Double.add() functions, I get 3 errors:

at java.lang.NumberFormatException.forInputString

at java.lang.Integer.parseInt

at java.lang.Integer.valueOf

I removed the Integer parsers, and the program worked fine. I'm confused as to why I get the errors only when I enter Decimal values and would like someone to help point out what exactly is causing the exception so I can avoid any errors like this in the future, and since removing the Integer parser removes any functionality from the IntegerN class.

Also, if anyone needs the FloatN class for whatever reason:

public static class FloatN implements Summization<Float>{
    public FloatN(){}
    public Float add(Float a, Float b)
    {
        return a + b;
    }
}

Summization is a generic interface with an add() method.

Thanks in advance.

解决方案

If you enter a decimal value as input, Integer.parseInt() method won't be able to parse it. If you still want to have them all in your code, you have to get int value of that Float value. You can use intValue() method:

    Float input2 = Float.parseFloat(in1);
    Integer input1 = Integer.valueOf(input2.intValue());

这篇关于将String解析为Float值时的NumberFormatException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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