i'm trying to get Double value from json, i want to ask how to check double value is empty or not?
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:
xcord = c.getDouble(kartu_xcord); ycord = c.getDouble(kartu_ycord); this is how i try to check xcord is null or empty :
if (xcord.isNaN()) { LinlayNoMap.setVisibility(View.VISIBLE); } else { linlaymap.setVisibility(View.VISIBLE); } i hope someone can help me to solve my proble thank you
27 Answers
In java double is a primitive type which can not be null, There is Double wrapper class in java which can be checked as null.
Declare your coords of type Double not double and check if Double value is null.
In Java double cannot be null.
It can't be null or empty. It's a double value. Maybe try the Double wrapper?
2You could check if your json has the key you area looking for:
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.
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 hasDouble method 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.
doubles can never be null in Java. You can instead use the hasKey method of Json to check if the item you're parsing contains the value.
c.hasKey("xcord") 2assume you are using JSONObject, you can do this
xcord = c.optDouble(kartu_xcord, Double.MIN_VALUE); if (xcord == Double.MIN_VALUE){ //xcord is empty } 1Use Double, not double.
Double dValue= someVariable; if(!dValue.isNaN()) { // you code here } 1