• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp bean problem, equal not working on strings  RSS feed

 
Dean Reedy
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is what I have:
<%@ page import = "UserLogin" %>
<jsp:useBean id="c" scope="session" class="UserLogin">
</jsp:useBean>
<%= c.getDescription() %><br>
<% String temp = c.getLevel(); %>
<% out.println(temp);
%>
<% if (temp == "admin" ) { %>
equal
<% } else { %>
not equal
<% } %>
Now why when temp equal "admin" why does the not equal part of the function get called. I have tried to trim temp, checked the length is 5 characters. But I see the word admin printed to the screen, yet the if function fails. Any ideas?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This seems to be the question of the month!
In Java, the == is a test for identity not equality. To test your string for equality you would use:

or better yet:

if temp can be null.
The fragment:

tests to see if temp is the same object as "admin"; not a test to see if the objects have the same value.
hth,
bear
[ March 20, 2002: Message edited by: Bear Bibeault ]
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!