-1
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    WorldDBManager DB = new WorldDBManager();
    String choices = request.getParameter("selectchoice");
    List<WorlPopulationInfo> country = new ArrayList<>();

    country = DB.getResultAsArrayList("world", "select * from country");


    StringBuffer strB = new StringBuffer();


    for(int i=0;i<country.size();i++){
        if(country.get(i).countryName == choices){
            strB.append("<option selected='selected'>" + country.get(i).countryName+"</option>");
            System.out.println(choices + " pareil " + country.get(i).countryName);
        }else if(country.get(i).countryName != choices){
            strB.append("<option>" + country.get(i).countryName+"</option>");
            System.out.println(choices + " " + country.get(i).countryName);
        }
    }


    request.setAttribute("country", strB);




    getServletContext().getRequestDispatcher("/index.jsp").forward(request,  response);

}

我不明白为什么他不去第一个如果条件我知道变量选择的值存在于我的数据库中,通过执行 System.println。我只是不明白为什么无视我的平等。如果有人可以解释我做错了什么。谢谢。

4

3 回答 3

2

尝试 -

if(country.get(i).countryName.equals(choices)){

你不需要else If那里,else就足够了

 if(country.get(i).countryName.equals(choices)){
            strB.append("<option selected='selected'>" + country.get(i).countryName+"</option>");
            System.out.println(choices + " pareil " + country.get(i).countryName);
        }else{
            strB.append("<option>" + country.get(i).countryName+"</option>");
            System.out.println(choices + " " + country.get(i).countryName);
        }
于 2013-04-08T23:52:28.713 回答
0

使用 .equals() 进行字符串比较。

于 2013-04-08T23:52:21.723 回答
0

我认为您不能将字符串与 == 进行比较(也许在 1.7 中)。你必须使用

if(country.get(i).countryName.equals(choices)){}
于 2013-04-08T23:53:46.977 回答