解析存储为字符串的浮点数应抛出异常 [英] Parsing float stored as string should throw Exception

查看:112
本文介绍了解析存储为字符串的浮点数应抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储号码的字符串。现在我想解析该字符串并获得浮点数。

I have a string which stores a number. Now i want to parse that string and get as float.

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        try {
            System.out.println(Integer.parseInt("  2  "));
        } catch(NumberFormatException e) {
            System.out.println("Exception caught");
        }
        System.out.println(Float.parseFloat("  2.4  "));

    }
}

现在在上面的代码中如果你运行它会成功的。我的问题是为什么在整数的情况下尾随空格抛出一个 NumberFormatException ,而解析一个浮点数并不抛出一个?

Now in the above code if you run it it will be successfull. My QUESTION is this why does trailing spaces in case of integer throws a NumberFormatException while parsing a float doesnt throw one ?

PS:booolean和double解析的情况相同。

PS: Same is the case with booolean and double parsing.

PPS:为什么java中存在不一致?我已经检查了源代码

PPS: Why is there an inconsistency in java ? And i already check the source code

推荐答案

正如您在相关源代码中看到的那样,该值将被修剪:

As you can see in the related source code, the value will be trimmed:

static FloatingDecimal.ASCIIToBinaryConverter readJavaFormatString(String arg) throws NumberFormatException {
    boolean arg0 = false;
    boolean arg1 = false;

    try {
        arg = arg.trim();
     ....

因此在转换为floatValue之前将删除空白。
有关更多信息,请参阅 FloatingDecimal ,由 Float.class 调用。

So the blanks will be removed before converting to a floatValue. For more informations see the sourcecode of FloatingDecimal which is called by Float.class.

Integer.parseInt()不要修剪字符串值:

public static int parseInt(String arg, int arg0) throws NumberFormatException {
    if (arg == null) {
        throw new NumberFormatException("null");
    } else if (arg0 < 2) {
        throw new NumberFormatException("radix " + arg0 + " less than Character.MIN_RADIX");
    } else if (arg0 > 36) {
        throw new NumberFormatException("radix " + arg0 + " greater than Character.MAX_RADIX");
    } else {
        int arg1 = 0;
        boolean arg2 = false;
        int arg3 = 0;
        int arg4 = arg.length();
        int arg5 = -2147483647;
        if (arg4 > 0) {
            char arg8 = arg.charAt(0);
            if (arg8 < 48) {
                if (arg8 == 45) {
                    arg2 = true;
                    arg5 = MIN_VALUE;
                } else if (arg8 != 43) {
                    throw NumberFormatException.forInputString(arg);
                }

                if (arg4 == 1) {
                    throw NumberFormatException.forInputString(arg);
                }

                ++arg3;
            }

            int arg7;
            for (int arg6 = arg5 / arg0; arg3 < arg4; arg1 -= arg7) {
                arg7 = Character.digit(arg.charAt(arg3++), arg0);
                if (arg7 < 0) {
                    throw NumberFormatException.forInputString(arg);
                }

                if (arg1 < arg6) {
                    throw NumberFormatException.forInputString(arg);
                }

                arg1 *= arg0;
                if (arg1 < arg5 + arg7) {
                    throw NumberFormatException.forInputString(arg);
                }
            }

            return arg2 ? arg1 : -arg1;
        } else {
            throw NumberFormatException.forInputString(arg);
        }
    }
}

这就是为什么你得到一个例外那里

Thats why you get an Exception there

这篇关于解析存储为字符串的浮点数应抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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