File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in HFSJ page-420

 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a doubt in question 6 given in HFSJ. I am not able to understand option 'E'. Can anybody explain me.

Thanks in advance.
 
Reesha Ashok
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will be good if you can post the question with the options.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is:
<% java.util.List list = new java.util.ArrayList();
list.add("a");
list.add("2");
list.add("c");
request.setAttribute("list",list);
request.setAttribute("listIdx","1");
%>
<%-- insert code here --%>


Which, inserted at the line will evaluate to c?


My question is why ${list{'listIdx'+1]) is invalid?

Please reply.


Thanks in advance.
 
Reesha Ashok
Ranch Hand
Posts: 32
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is why ${list{'listIdx'+1]) is invalid?


Because 'listIdx'+1 is a String value which cannot be converted to integer. List and ArrayList does index search. EL can coerce String to integer value in certain cases which means ${list[1]} is same as ${list["1"]}. But this conversion is not possible with ${list['listIdx'+1]}.
However, Had the expression been ${list[listIdx+1]}, it would have been a valid expression.Reason being listIdx set as an attribute in Request. request.setAttribute("listIdx","1");
In this scenario, listIdx since it's not in quotes would evaluate to the value set in request which is "1" and the expression becomes ${list["1"+1]} which is same as ${list[1+1]} or ${list[2]} which is perfectly valid.

Reading pages 372 and 375 of HFSJ should give you more insight into this.
 
Padma priya Gururajan
Ranch Hand
Posts: 437
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Reesha,
How does 'listIdx'+1 become a string? Does 'listIdx' hold a value of 1?

Thanks in advance.
 
swapna rao
Ranch Hand
Posts: 53
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Padma,
Any content within quotes will be considered as String and the same rule applies to listIdx. If you use listIdx without quotes , the variable value will be evaluated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic