Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have a Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edition of chapter 8.
The same problem is in page 380 of 2nd edition.

Solution given is:


I think the solution should be

or




Can someone please explain me why there is no " " inside the [1] and [2]?

Also, why do we have to use the .name after [] to access the name of the toy when I thought we can directly do so using toys[0]?

Thanks in advance for your help and cooperation.

 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside [], you can access indexed elements with or without "". So If I have an array in page scope named myArr, then ${myArr[0]} and #{myArr["0"]} mean the same thing.

As far as the second solution goes, I can't say whether it is wrong or not as I don't have the book right now. But the thing is, if you use .name on the toy objects, then it will display the value of the name property of the toy objects. If you don't include the .name, then the toString method of the toy objects will be called and whatever that returns, will be displayed. So if the toString method of Toy class returns the name of the toy, then the last code will also produce the correct output...
 
Oprah Thaddeus
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Thanks..But I have some confusion still..

I think [1] and ["1"] are not the same thing as [mybooks] and ["mybooks"] are different. Correct me if I am wrong
 
Ankit Garg
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:inside [], you can access indexed elements with or without "". So If I have an array in page scope named myArr, then ${myArr[0]} and #{myArr["0"]} mean the same thing.


See what I wrote again. For indexed elements i.e. ArrayList, Array etc, [1] and ["1"] mean the same thing...
 
No thanks. We have all the government we need. This tiny ad would like you to leave now:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic