2013-02-24 26 views
1

嗨,我試圖解決這個小誤差與正在顯示的是無法找到符號方法修剪() 和位置修剪(),錯誤 消息是可變價格類型的雙。以下是代碼。/* *要更改此模板,請選擇工具|模板*並在編輯器中打開模板。 * /包助手;無法找到裝飾()進行變價雙方法

import bean.ProductBean; import java.sql.ResultSet; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import javax.servlet.http.HttpServletRequest;

公共類ProductHelper {

static final SimpleDateFormat SDF = new SimpleDateFormat("dd/MM/yyyy"); 

public static void populateaddproduct(ProductBean addproduct, HttpServletRequest request) throws ParseException { 
    String rowid = request.getParameter("rowid"); 
    if (rowid != null && rowid.trim().length() > 0) { 
     addproduct.setRowid(new Integer(rowid)); 
    } 
    addproduct.setEan(request.getParameter("ean")); 
    addproduct.setPip(request.getParameter("pip")); 
    addproduct.setName(request.getParameter("name")); 
    addproduct.setDescription(request.getParameter("description")); 
    addproduct.setSupplier(request.getParameter("supplier")); 
    **Double price = Double.parseDouble(request.getParameter("price")); 
    if (price != null && price.trim().length() > 0) { 
     addproduct.setPrice(new Double(price));** 
    } 
    String expiryDate = request.getParameter("expirydate"); 
    if (expiryDate != null && expiryDate.trim().length() == SDF.toPattern().length()) { 
     addproduct.setExpiryDate(SDF.parse(expiryDate)); 
    } 
    addproduct.setLatest(request.getParameter("latestproduct")); 
    addproduct.setDiscounted(request.getParameter("discount")); 

} 

public static void populateProduct(ProductBean product, ResultSet rs) throws SQLException { 
    product.setRowid(rs.getInt("id")); 
    product.setEan(rs.getString("ean")); 
    product.setPip(rs.getString("pip")); 
    product.setName(rs.getString("name")); 
    product.setDescription(rs.getString("description")); 
    product.setSupplier(rs.getString("supplier")); 
    product.setPrice(rs.getDouble("price")); 
    product.setExpiryDate(rs.getDate("expirydate")); 
    product.setLatest(rs.getString("latestproduct")); 
    product.setDiscounted(rs.getString("discount")); 

} } 
+0

確定,所以我怎樣才能解決這個雙? – user1948682 2013-02-24 01:19:22

+0

錯誤是完全正確的。你試圖做的事實際上沒有任何意義。 – SLaks 2013-02-24 01:20:08

回答

0

價格Double類型。 java.lang.Double沒有trim()方法。 trim()是刪除前導和尾隨空格的方法java.lang.String

我認爲在這裏的空檢查很好,你並不需要檢查長度。

if (price != null) 
1

trim()方法刪除的前導和結尾的空白。 double的類型是數字 - 空格的概念不適用於它。您不需要修剪double - 它總是隱式修剪。

如果您閱讀String代表 a double然而,您可能需要修剪它。變量的類型需要爲String,而不是Double,以便應用trim()方法。顯然,如果您需要在以後使用的值作爲double,則需要通過調用valueOf方法將字符串轉換爲double,例如:

String priceStr = request.getParameter("price"); 
if (priceStr != null && priceStr.trim().length() != 0) { 
    addproduct.setPrice(Double.valueOf(priceStr)); 
} 
+0

謝謝這麼多!你完美地解釋了它。 – user1948682 2013-02-24 01:30:26