NumberFormatException when parsing String to Float value [duplicate]

喜欢而已 提交于 2019-12-24 19:29:52

问题


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.


回答1:


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());



回答2:


Maybe add string which is not contain a parsable float or it is null.

From javadoc:

NullPointerException - if the string is null
NumberFormatException - if the string does not contain a parsable float.



回答3:


becuase Integer.valueOf(in2); this line will give NumberFormatException with float and double you can use

Number num = NumberFormat.getInstance().parse(myNumber);

see @ http://docs.oracle.com/javase/7/docs/api/java/text/NumberFormat.html




回答4:


The NumberFormatException comes in the Integer parser, and not in the Double and Float parsers if input contains decimal.

In such situations, you can get the the integer part of the decimal number using split and parse:

    String[] s = in1.split("\\.");
    Integer input1 = Integer.valueOf(s[0]);
    Float input2 = Float.parseFloat(in1);
    Double input3 = Double.valueOf(in1).doubleValue(); 


来源:https://stackoverflow.com/questions/20537121/numberformatexception-when-parsing-string-to-float-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!