java - 找不到可变价格 double 的修剪()方法

标签 java

Hi, Im trying to fix this small error with the trim().The error message that is being displayed is cannot find symbol method trim() and location is of variable price type double. Below is the code. /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package helper;

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

public class 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"));

} }

最佳答案

trim() 方法从字符串中删除前导和尾随空格。 double 的类型是数字 - 空白的概念本身不适用于它。您不需要修剪double - 它总是被隐式修剪。

但是,如果您读取的String代表一个double,则可能需要修剪它。变量的类型必须是 String,而不是 Double,以便应用 trim() 方法。显然,如果稍后需要将该值用作 double,则需要将字符串转换为 double,例如通过调用 valueOf方法:

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

关于java - 找不到可变价格 double 的修剪()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047652/

相关文章:

Java synchronized 似乎被忽略了

java - GWT CSS 语法无法正常工作

java - 什么是原始类型,为什么我们不应该使用它呢?

java - Tomcat6 日志中出现奇怪的 struts2 异常(没有为操作名称映射的操作 [一些 HTML 代码!!!!]。)

java - 查找两个数组之间的差异

java - 连接到互联网 : Google Custom Search in java app "not connecting" ()

Java 扫描器验证返回第二个输入

java - JavaFX Triangle Mesh Pyramid - 它有效,但为什么呢?

java - 使用堆栈在 java 中获取 infix/postfix 的错误输出

java - 如何在java bean和Jackson Json中创建VarArgs?