Java set values after if else loop in one line

  • A+
Category:Languages

How do I achieve setting the value inside if else in one line?

String amount = myJSON.optString("cValue") double cValue = (amount != null && !amount.isEmpty()) ? Double.parseDouble(amount) : 0; if (cValue > 0) {     mySavings.setCouponValue(cValue); } else {     mySavings.setCouponValue(0.0); } 

 


You could simply do :

mySavings.setCouponValue((cValue > 0 ? cValue : 0.0)); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: