数据类型,如?是在java中的类型

时间:2022-08-28 16:22:21

I'm trying to verify my parseInt() and parseFloat() has worked they do print out the changed data. It'd be nice if there were a isTypeOf style test to prove String has became an integer. Is there anything similar in java?

我正在尝试验证我的parseInt()和parseFloat()工作,他们打印出更改的数据。如果有一个isTypeOf样式测试来证明String已经变成一个整数,那就太好了。 java中有类似的东西吗?

2 个解决方案

#1


2  

Well, you can use a Float.parseFloat(String) and catch a NumberFormatException like this:

好吧,您可以使用Float.parseFloat(String)并捕获NumberFormatException,如下所示:

public boolean isAnInt(String number)
{
    try {
        Integer.parseInt(number);
    } catch(NumberFormatException ex) {
        return false;
    }
    return true;
}

public boolean isFloat(String number)
{
    try {
        Float.parseFloat(number);
    } catch(NumberFormatException ex) {
        return false;
    }
    return true;
}

I think there are better ways but I don't remmeber right now.

我认为有更好的方法,但我现在不记得了。

#2


1  

parseInt and parseFloat will throw a NumberFormatException if something went wrong.

如果出错,parseInt和parseFloat将抛出NumberFormatException。

#1


2  

Well, you can use a Float.parseFloat(String) and catch a NumberFormatException like this:

好吧,您可以使用Float.parseFloat(String)并捕获NumberFormatException,如下所示:

public boolean isAnInt(String number)
{
    try {
        Integer.parseInt(number);
    } catch(NumberFormatException ex) {
        return false;
    }
    return true;
}

public boolean isFloat(String number)
{
    try {
        Float.parseFloat(number);
    } catch(NumberFormatException ex) {
        return false;
    }
    return true;
}

I think there are better ways but I don't remmeber right now.

我认为有更好的方法,但我现在不记得了。

#2


1  

parseInt and parseFloat will throw a NumberFormatException if something went wrong.

如果出错,parseInt和parseFloat将抛出NumberFormatException。