0
if(student_code.substring(0,3 )=="MLV")
  count1++;

But count1 always return 0

Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
Sukanta Pal
  • 33
  • 1
  • 1
  • 6
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – takendarkk Aug 23 '15 at 16:00

3 Answers3

9
if(student_code.substring(0,3 )=="MLV")
  count1++;

This doesn't look like a JSP code. It looks more of a scriptlet in JSP, which is nothing but java code. If that's the case, you still need to use equals for string comparison, like

if(student_code.substring(0,3 ).equals("MLV"))
      count1++;

If you want to substring and compare strings in JSP, use JSTL functions as shown below

<c:set var="mystring" value="<%=student_code%>"/>

<c:if test="${fn:substring(mystring, 0, 3) == 'MLV'}">
     <%count1++;%>
<c:if>

Also for above JSTL code to work you would need to import below taglibs in JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
0

You can use the '==' symbols to compare two strings in JSP, I would put spaces inbetween and change the double quotes to singles, e.g:

if(student_code.substring(0,3 ) == 'MLV')

Hopefully this helps.

Reference post: How to compare two object variables in EL expression language?

Community
  • 1
  • 1
Masutatsu
  • 434
  • 1
  • 7
  • 19
  • 1
    Please read this post [How do I compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) and understand why your statement `You can use the '==' symbols to compare two strings` should come with a disclaimer. – takendarkk Aug 24 '15 at 03:50
  • 1
    in jsp is valid, is translated to Object.equals() – ejaenv Feb 07 '18 at 19:02
0

Oho friends .... I just change my code to

str1=student_code.substring(0,3 ).trim();
if(str1.equals("YML"))
  count1++;

..and its working

Sukanta Pal
  • 33
  • 1
  • 1
  • 6