Java 如何检查double值是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19133412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:23:46  来源:igfitidea点击:

how to check double value is empty or not

javaandroidjson

提问by Aoyama Nanami

i'm trying to get Double value from json, i want to ask how to check double value is empty or not?

我正在尝试从 json 中获取 Double 值,我想问一下如何检查 double 值是否为空?

this is how i initial it private static

这就是我初始化它私有静态的方式

final String kartu_xcord = "xcord";
    private static final String kartu_ycord = "ycord";
    ouble xcord,ycord;

this is how i parse my json:

这就是我解析我的 json 的方式:

xcord = c.getDouble(kartu_xcord);
ycord = c.getDouble(kartu_ycord);

this is how i try to check xcord is null or empty :

这就是我尝试检查 xcord 是空还是空的方式:

if (xcord.isNaN()) {
            LinlayNoMap.setVisibility(View.VISIBLE);
        } else {
            linlaymap.setVisibility(View.VISIBLE);
}

i hope someone can help me to solve my proble thank you

我希望有人能帮我解决我的问题谢谢

采纳答案by nsgulliver

In java doubleis a primitive type which can not be null, There is Doublewrapper class in java which can be checked as null.

在java中double是一个不能为null的原始类型,java中有 Double包装类可以检查为null。

Declare your coords of type Doublenot doubleand check if Doublevalue is null.

声明您的坐标类型Doublenotdouble并检查Double值是否为空。

回答by Arash GM

In Java doublecannot be null.

在 Javadouble中不能是null.

回答by Kayaman

It can't be null or empty. It's a double value. Maybe try the Double wrapper?

它不能为 null 或为空。这是双重价值。也许试试双包装?

回答by josephus

doubles can never be null in Java. You can instead use the hasKeymethod of Json to check if the item you're parsing contains the value.

在 Java 中双精度永远不能为空。您可以改为使用hasKeyJson的方法来检查您正在解析的项目是否包含该值。

c.hasKey("xcord")

回答by Vincent Mimoun-Prat

You could check if your json has the key you area looking for:

您可以检查您的 json 是否具有您要查找的密钥:

if (c.hasDouble("my_coord")) {
  y_coord = c.getDouble("my_coord");
}

if y_coord is a Double(with capital D), you can set it to null.

如果 y_coord 是 a Double(大写 D),则可以将其设置为 null。

if (c.hasDouble("my_coord")) {
  y_coord = c.getDouble("my_coord");
} else {
  y_coord = null;
}

This depends of course on the API you are using to read your JSON, the hasDoublemethod could be called something else. I recommend you give a try to GSON, very simple and will handle most of the parsing work for you.

这当然取决于你用来读取你的 API JSON,该hasDouble方法可以被称为其他东西。我建议您尝试一下GSON,非常简单并且会为您处理大部分解析工作。

回答by M Atif

Use Double, not double.

使用双,而不是双。

Double  dValue= someVariable;

if(!dValue.isNaN())
{
 // you code here
}

回答by william

assume you are using JSONObject, you can do this

假设您使用的是 JSONObject,您可以这样做

xcord = c.optDouble(kartu_xcord, Double.MIN_VALUE);

if (xcord == Double.MIN_VALUE){
  //xcord is empty
}