下面我有以下代碼:Java的字符串轉換到整數
private Integer mTarifId;
public Integer getTarifId() {
return mTarifId;
}
public void setTarifId(Integer tarifId) {
this.mTarifId = tarifId;
}
我已經初始化了一個名爲「mTarifId」變量。我需要在下面的代碼中使用它。
if (!Objects.equals(mTarifId, null)) {
query = mEntityManager.createQuery("FROM TarifListeCalendrierEntity WHERE tarifListeId=:pId")
.setParameter("pId", mTarifId);
List<TarifListeCalendrierEntity> tarifListCalendrierList = query.getResultList();
if (!tarifListCalendrierList.isEmpty()) {
TarifListeCalendrierEntity tarifListCalendrier = tarifListCalendrierList.get(0);
request.setAttribute("tarif_list_calendrier", tarifListCalendrier);
}
}
但爲了做到這一點,我需要將其轉換爲一個整數「的doGet」
String tarifId = request.getParameter("tarifid");
if (!Objects.equals(tarifId, null)) {
setTarifId(Integer.valueOf(tarifId));
}else {
setTarifId(null);
}
請幫助識別我在做什麼錯。
請描述錯誤你是從代碼示例獲得。 –
當調試這行代碼時跳過'if(!Objects.equals(mTarifId,null)){'表示mTarifId的值仍爲null。 – Fadil
一個朋友告訴我用這樣的東西在mTarifId獲得價值 mTarifId =的request.getParameter(「tarifid」) 但是,當這樣做,我發現了錯誤不兼容的類型:字符串不能轉換爲整數 – Fadil