Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating Hashtable key is not working

Tags:

java

jsp

jstl

I am trying to access a Hashtables value based on its key, which is a number as String in JSTL. But if I increment/decrement the keys value, it does not work anymore.

I iterate the sorted list of keys in a for loop. I use this item to access Hashtable.

<c:forEach items="${helper:getSortedList(hashtableObj)}" var="lineNumber" varStatus="loop">
    <c:if test="${param.lineNbr eq lineNumber}">
        <c:if test="${lineNumber>1}">
            <fmt:parseNumber var="prevLineNumberKey" type="number" value="${lineNumber-1}" />
            <c:out value="PREV ${hashtableObj[prevLineNumberKey]}" escapeXml="false"/><br/>
        </c:if>
        <c:out value="Current :${lineNumber}" /><br/>
        <c:if test="${lineNumber<fn:length(hashtableObj)-1}">
            <fmt:parseNumber var="nextLineNumberKey" type="number" value="${lineNumber+1}" />
            <c:out value="NEXT ${hashtableObj[nextLineNumberKey+1]}" escapeXml="false"/><br/>
        </c:if>
</c:if>
</c:forEach>

The output is

PREV
Current :51
NEXT

But what I expected is

PREV 50
Current :51
NEXT 52

Any pointers are appreciated.

like image 644
bkrish Avatar asked May 20 '26 03:05

bkrish


1 Answers

If keys in your Map is String than to get an element you must query it with String value. Your current solution queries a Map with Long value.
You can convert number to String and then query a Map like this:

<c:set var="numberAsString">${50 - 1}</c:set>
<c:out value="value: ${hashtableObj[numberAsString]}"/>
like image 64
madox Avatar answered May 21 '26 16:05

madox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!