-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);

}

i don't understand why he doesnt go to the first if condition i know the value from variable choices exist inside my database by doing a System.println. i just don't understand why is ignoring my equality. If someone could explain me what im doing wrong. thank.

DarkVision
  • 1,373
  • 3
  • 20
  • 33
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Philipp Wendler Mar 09 '14 at 11:27

3 Answers3

2

Try -

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

You don't need else If there, only else would be sufficient

 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);
        }
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
0

Use .equals() for string comparisons.

Madeline
  • 45
  • 3
0

I think you can´t compare string with == (maybe in 1.7). You have to use

if(country.get(i).countryName.equals(choices)){}
Mitch Bukaner
  • 171
  • 13