wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt" Watch "Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt" New topic
Author

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

Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
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.


Working on my SCWCD so I can be a J2EE consultant earning millions of dollars and showing everyone I can
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Swagato Bhatta
Ranch Hand

Joined: Nov 08, 2008
Posts: 72
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem with the Scriptless JSps Page 378 question (Code Magents) from the Head First book 1st edt