3

我试图通过严格的 Java 方法和运算符挖掘我的方式,但现在,试图将一段 PHP 代码“翻译”为 Java (Android),我有点卡住了。

在 PHP 中:

if ($row['price']>'0'){
  (.. do something if price is defined - and higher than zero ..)
}

问题是 $row['price'] 可能为空(在 Java 中:null?)或包含 '0'(零)。但是我怎样才能以一种智能且不太复杂的方式在 Java 中编写代码呢?

4

2 回答 2

6

假设您以可变价格获得价格字符串

String price = <get price somehow>;    
try {
    if (price != null && Integer.valueOf(price) > 0) {
        do something with price...
    }
} catch (NumberFormatException exception) {
}
于 2012-05-13T17:42:42.090 回答
2

你可以使用这个:

String price="somevalue";
int priceInt=Integer.valueOf(price);

try{
if( !price.equals("") && priceInt>0){

// if condition is true,do your thing here!

}
}catch (NullPointerException e){

//if price is null this part will be executed,in your case leave it blank
}
catch (NumberFormatException exception) {
}
于 2012-05-13T18:26:40.243 回答