File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes charAt() Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "charAt() Query" Watch "charAt() Query" New topic
Author

charAt() Query

Kancharla Madhu
Ranch Hand

Joined: May 06, 2012
Posts: 109

The following statement is with respect to chartAt() of String,This statement looks true to me but it was given as false in one of the mock test. It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). Could someone confirm whether it is true or false.

Champions arenĀ“t made in the gyms. Champions are made from something they have deep inside them - a DESIRE, a DREAM, a VISION
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

It's false.

But you would like to know why, wouldn't you? So don't guess, go and look at the API documentation for the method. It tells you there what exceptions are thrown and when.
Kancharla Madhu
Ranch Hand

Joined: May 06, 2012
Posts: 109

As per the below code snippet the statement is TRUE . ( It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0). )

Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
Class String / charAt says -


Throws:
IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.


Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
James X Peterson
Whizlabs Java Support
Ranch Hand

Joined: Feb 26, 2013
Posts: 158
Hi Kancharla Madhu,

String str="abcd"

Try to print index of a b c d you will get 0 1 2 3 respectively. If you try chatAt() method other than 0 1 2 3 you will get IndexOutOFBoundException.

Regards,
James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: charAt() Query