ArrayList queryLeagueId = dDao.queryLeagueId(); //这个是我从数据库中获取联赛表中联赛ID的所有数据int leagueId=addTeamForm.getLeagueId(); //这个是我获取页面输入的值,也就是用户填写的联赛ID(球队表中)我如何做判断呢?也就是说 如何在联赛表中联赛ID只有1,2,3,4
要在球队表中用户输入1,2,3,4以外的值,就显示错误信息,关键联赛表中联赛的ID如何做。他是ArrayList 类型 着急!谢谢各位了
要在球队表中用户输入1,2,3,4以外的值,就显示错误信息,关键联赛表中联赛的ID如何做。他是ArrayList 类型 着急!谢谢各位了
boolean isCorrectId = false;//check if leagueId is in the list
for(int i=0; i<queryLeagueId.size(); i++) {
int tempId = Integer.parseInt((String)queryLeagueId.get(i));
if(leagueId == tempId) {
isCorrectId = true;
break;
}
}//right here, if isCorrectId is true, leagueId is correct, otherwise is wrong.
你要设置一个布尔类型标志变量,并且默认为false,
定义一个循环,获取联赛id,
再用你获取的用户输入的联赛id与之想比较,
若相等,把布尔类型标志变量设置为true并退出,
若不相等,则继续循环!
{
return true;
}
else
{
return false;
}
提示类转换异常
javax.servlet.ServletException: java.lang.ClassCastException: com.gp.struts.League
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause
if (queryLeagueId.contains(Integer.toString(leagueId)))
{
return true;
}
else
{
return false;
}但是貌似,contains接受的是object,应该不会报错的。最多是结果不对。
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AddTeamForm addTeamForm = (AddTeamForm) form;
int leagueId=addTeamForm.getLeagueId();
String teamName=addTeamForm.getTeamName();
String teamWeb=addTeamForm.getTeamWeb();
String teamRes=addTeamForm.getTeamRes();
DatabaseDao dDao= new DatabaseDao();
ArrayList queryLeagueId=dDao.queryLeagueId();
boolean addTeam=dDao.addTeam(leagueId, teamName, teamWeb, teamRes);
return mapping.findForward("index");
}
}这个就是我写的...就是不知道leagueId和queryLeagueId怎么去比较
我要判断leagueId是不是queryLeagueId里的值
btw, 你的代码有bad smell.