こんにちは、私はこの小さなエラーをtrim()で修正しようとしています。表示されているエラーメッセージはシンボルメソッドtrim()が見つからず、場所は可変価格タイプdoubleです。以下はコードです。/ * *このテンプレートを変更するには、[ツール]|[ツール]を選択します。テンプレート*そしてエディターでテンプレートを開きます。*/パッケージヘルパー;
インポートbean.ProductBean; import java.sql.ResultSet; インポートjava.sql.SQLException; インポートjava.text.ParseException; import java.text.SimpleDateFormat; インポート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")); } }
1735 次
2 に答える
1
このメソッドは、文字列trim()
から先頭と末尾の空白を削除します。のタイプは数値です。空白の概念そのものは適用されません。トリミングする必要はありません-常に暗黙的にトリミングされます。double
double
ただし、String
を表すを読み取る場合はdouble
、トリミングする必要があります。メソッドを適用するには、変数のタイプがではString
なくDouble
、である必要があります。trim()
明らかに、後で値を使用する必要がある場合はdouble
、たとえば次のvalueOf
メソッドを呼び出して、文字列をdoubleに変換する必要があります。
String priceStr = request.getParameter("price");
if (priceStr != null && priceStr.trim().length() != 0) {
addproduct.setPrice(Double.valueOf(priceStr));
}
于 2013-02-24T01:19:55.700 に答える
0
価格はタイプDouble
です。java.lang.Double
メソッドがありませんtrim()
。trim()
の先頭と末尾のスペースを削除する方法ですjava.lang.String
ここではnullチェックだけで問題ないと思います。実際に長さをチェックする必要はありません。
if (price != null)
于 2013-02-24T01:19:29.603 に答える