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/