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 に答える