我可以在字符串串联中使用条件三元运算符吗? [英] Can I use the conditional ternary operator into a string concatenation?
本文介绍了我可以在字符串串联中使用条件三元运算符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下疑问:我可以通过某种方式在字符串连接中使用条件三元运算符吗?
I have the following doubt: can I in some way use the conditional ternary operator into a string concatenation?
我试图做这样的事情:
String sql = "insert into TirConsolidatoPolizzaFondo "
+ "(Polizzaid, FondoID, isQuadraturaOk, ReteVendita, CodiceConferimento, PercentualeRendimentoDaInizioGestione, "
+ "DataInizioGestione, PercentualeRendimentoDaInizioAnno, DataInizioAnno, PercentualeRendimentoDaInizioTrimestre, "
+ "DataInizioTrimestre, DataCalcoloBF, ValoreNavBF, DataRiferimentoNavBF, ControvaloreFinaleBF, ValoreNavPUC, "
+ "DataRiferimentoNavPUC, ControvaloreFinalePUC, NumeroQuotePUC, DeltaControvaloreFinale, TIMESTAMP, "
+ "DataValutaUltimaOperazione, MsgPercentualeRendimentoDaInizioAnno, MsgPercentualeRendimentoDaInizioGestione, "
+ "MsgPercentualeRendimentoDaInizioTrimestre, isTirAnomalo, CodiceLineaBF) "
+ "values"
+ "(" +
qsTirPF.getPolizzaid() != null ? qsTirPF.getPolizzaid() : "null";
这意味着在第一个String主干中,我想连接由以下项计算的值:
that means that at the first String trunk I want to concatenate the value evaluated by:
qsTirPF.getPolizzaid() != null ? qsTirPF.getPolizzaid() : "null";
但是Eclipse会向我指出此错误:
but Eclipse sign me this error:
Type mismatch: cannot convert from Object&Comparable<?>&Serializable to String
为什么?怎么了?我该如何解决这个问题?
Why? What is wrong? How can I fix this issue?
推荐答案
首先,您应该将三元条件放在方括号中:
First you should put your ternary condition inside brackets :
(qsTirPF.getPolizzaid() != null ? qsTirPF.getPolizzaid() : "null")
此外,您还应该验证qsTirPF.getPolizzaid()是否返回字符串
Furthermore you should verify that qsTirPF.getPolizzaid() is returning a string
这篇关于我可以在字符串串联中使用条件三元运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文