setPrice(double) in **cannot be applied to (java.lang.String) at
"NewCD.java":setPrice(double)inmyshop.model.CDcannotbeappliedto(java.lang.String)atli...
"NewCD.java": setPrice(double) in myshop.model.CD cannot be applied to (java.lang.String) at line 45, column 4
错误是这样
代码是
package myshop.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import myshop.model.*;
import myshop.Dao.*;
public class NewCD
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String CDName = request.getParameter("CDName");
if (CDName == null) {
CDName = "";
}
String price =(String)DBUtil.chaStr(request.getParameter("price"));
if (price == null) {
price = "";
}
String introduce =DBUtil.chaStr(request.getParameter("introduce"));
if (introduce == null) {
introduce = "";
}
String imageFile =DBUtil.chaStr(request.getParameter("imageFile"));
if (imageFile == null) {
imageFile = "";
}
String criticism =DBUtil.chaStr(request.getParameter("criticism"));
if (criticism == null) {
criticism = "";
}
HttpSession session = request.getSession();
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
CD CD = new CD();
CD.setCDName(CDName);
CD.setPrice(price);//---------------这里编译不通过
CD.setIntroduce(introduce);
CD.setImageFile(imageFile);
CD.setCriticism(criticism);
CDDAO dao = new CDDAO();
if (dao.FindNewCD(CD)) {
if (dao.addCD(CD)) {
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加成功!\")");
out.println("window.open(\"NewCD.jsp\",\"_self\")");
out.println("</script>");
}
else {
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加失败!\")");
out.println("window.open(\"NewCD.jsp\",\"_self\")");
out.println("</script>");
}
}else{
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加出现意外!!\")");
out.println("window.open(\"Register.jsp\",\"_self\")");
out.println("</script>");
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
} 展开
错误是这样
代码是
package myshop.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import myshop.model.*;
import myshop.Dao.*;
public class NewCD
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String CDName = request.getParameter("CDName");
if (CDName == null) {
CDName = "";
}
String price =(String)DBUtil.chaStr(request.getParameter("price"));
if (price == null) {
price = "";
}
String introduce =DBUtil.chaStr(request.getParameter("introduce"));
if (introduce == null) {
introduce = "";
}
String imageFile =DBUtil.chaStr(request.getParameter("imageFile"));
if (imageFile == null) {
imageFile = "";
}
String criticism =DBUtil.chaStr(request.getParameter("criticism"));
if (criticism == null) {
criticism = "";
}
HttpSession session = request.getSession();
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
CD CD = new CD();
CD.setCDName(CDName);
CD.setPrice(price);//---------------这里编译不通过
CD.setIntroduce(introduce);
CD.setImageFile(imageFile);
CD.setCriticism(criticism);
CDDAO dao = new CDDAO();
if (dao.FindNewCD(CD)) {
if (dao.addCD(CD)) {
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加成功!\")");
out.println("window.open(\"NewCD.jsp\",\"_self\")");
out.println("</script>");
}
else {
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加失败!\")");
out.println("window.open(\"NewCD.jsp\",\"_self\")");
out.println("</script>");
}
}else{
out.println("<script type=\"text/javascript\">");
out.println("alert(\"CD添加出现意外!!\")");
out.println("window.open(\"Register.jsp\",\"_self\")");
out.println("</script>");
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
} 展开
3个回答
展开全部
你的CD类中的setPrice(double x)方法中的参数类型是double的,而你此处price 为:
String price =(String)DBUtil.chaStr(request.getParameter("price"));
将String类型的price set给double类型,当然是不对的。
CD.setPrice(Double.valueOf(price));
另外你上面这样得到String 类型的price,这样的写法不太好,尽量学会用StringUtils类中的方法。
String price =(String)DBUtil.chaStr(request.getParameter("price"));
将String类型的price set给double类型,当然是不对的。
CD.setPrice(Double.valueOf(price));
另外你上面这样得到String 类型的price,这样的写法不太好,尽量学会用StringUtils类中的方法。
展开全部
需要类型转换
String price =(String)DBUtil.chaStr(request.getParameter("price"));
你已经定义price为String类型的了
但你的CD类中的setPrice却是Long类型的
把String类型的值赋给Long类型要类型转换的
CD.setPrice(Long.parseLong(price));
这样就没问题了。
String price =(String)DBUtil.chaStr(request.getParameter("price"));
你已经定义price为String类型的了
但你的CD类中的setPrice却是Long类型的
把String类型的值赋给Long类型要类型转换的
CD.setPrice(Long.parseLong(price));
这样就没问题了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
他不是告诉你了么 setPrice(double)的参数是double的 你的price是String类型的 所以不行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询