Results 1 to 7 of 7
- 02-04-2011, 06:30 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Java Official Documentation Error?
I'm total a newbie to Java, but I think I found something wrong with the documentation concerning with methods coming with object String.
URL:String (Java 2 Platform SE v1.4.2)
The method I'm referring to is indexOf with parameters of a char and a int indicating the index of the char to start the search. It says:
" If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned."
Here I think it should be put:
" If it is greater than the length of this string minus 1, it has the same effect as if it were equal to the length of this string: -1 is returned."
Since the last char in the String is with index of length()-1, so if fromIndex=length()-1 there is a hope if the last one is just the prey. If it is greater than that, there is definitely no hope, as same as to equal to length. Greater than is definitely not greater or equal than, is it?:mad:
So did I put it correct? If I'm wrong at any point, please tell me. I found the index extremely recalcitrant, so I wish to tackle this problem.
- 02-04-2011, 06:38 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
Best way to answer this is to try it yourself. Write a test case and see that the behavior is what the javadoc says it is. This makes perfect sense to me. The length of the string is out of bounds, and anything great will behave the same way.
- 02-04-2011, 08:09 PM #3
i suppose you do it right. in java the indexes are usually zero-based.
- 02-04-2011, 08:20 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
Yup, that was kind of sloppy; it better should've read 'if it was greater or equal to the length of the String'.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-04-2011, 11:26 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
It looks OK to me.
There are three cases to be considered:
(a) (Strictly) Greater than length: proceed as if equal to length
(b) Equal to length: return -1
(c) Otherwise: return index of match
The statement "If it is greater than the length of this string, it has the same effect as if it were equal to the length of this string: -1 is returned." contains within itself the statement of what will happen when it is equal to length. So the statement is exactly equivalent to "If it is greater than or equal to..." or "If it greater than length-1..."
If it was sloppy it was so only to the extent that it was clumsy.
- 02-04-2011, 11:32 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
The 1.6 documentation not only adopts the more transparent "if it greater than or equal to" but also deals with the case that the index is negative. (So I was wrong: there are four cases...)
The most important thing is also highlighted: any (int) value whatsoever may be used as an argument without error.
- 02-05-2011, 05:24 AM #7
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Attaching the Java documentation
By Eranga in forum NetBeansReplies: 11Last Post: 08-09-2010, 04:17 AM -
SCJA Exam Voucher and Official Sun Learning CD-ROMS for Sale
By Dutch_C0urage in forum Java CertificationReplies: 0Last Post: 05-21-2009, 09:26 PM -
SCJA Exam Voucher and Official Sun Learning CD-ROMS for Sale
By Dutch_C0urage in forum Reviews / AdvertisingReplies: 0Last Post: 05-03-2009, 02:46 PM -
Proofread my Java documentation?
By javanewbie in forum New To JavaReplies: 1Last Post: 06-24-2008, 08:39 AM -
Java Documentation
By ravian in forum New To JavaReplies: 4Last Post: 12-04-2007, 09:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks