0

I have this wierd problem with my servlet, I can't delete an element of an object if I pass variable to the delete method, but if I pass a String that contains an element that already exists it works like a charm. Here's my code:

The delete method:

public void delete(String t) {
    for (int i=0; i<list.size(); i++) {
        if(list.get(i).getTitre() == t) list.remove(i); 
    }
}

My servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    String titre = (String) request.getParameter("T1");   
    Biblio b = (Biblio) request.getSession().getAttribute("list");     

    // here this works which means "titre" isnt null
    b.add(titre, "b", 1); 

    // this also works which means the delete method works correctly 
    b.delete("a"); 

    // this doesn't work, i get no error, the page load but the item is still there 
   b.delete(titre);

   request.getSession().setAttribute("list", b);    

   this.getServletContext().getRequestDispatcher("/WEB-INF/main.jsp")
       .forward(request, response);
}

EDIT

The issue was quiet simple: I should have used equals instead of == in my delete method.

cassiomolin
  • 124,154
  • 35
  • 280
  • 359
Hamza Tahiri
  • 488
  • 3
  • 13
  • you dont compare Strings using `==`. use `equals` or `equalsIgnoreCase` if case sensitivity does not matter – jmcg Nov 17 '15 at 03:43
  • well that's embarrassing, i've used equals and i got a javanulpointer but apparently equals wasnt the problem, thanks a lot – Hamza Tahiri Nov 17 '15 at 03:49
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – worpet Nov 18 '15 at 14:39

1 Answers1

3

dont compare Strings using ==. use equals or equalsIgnoreCase if case sensitivity does not matter

jmcg
  • 1,547
  • 17
  • 22