Results 1 to 7 of 7
  1. #1
    afunuks is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    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.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    i suppose you do it right. in java the indexes are usually zero-based.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,434
    Blog Entries
    7
    Rep Power
    20

    Default

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  7. #7
    afunuks is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    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.
    That make sense!! I mean it just gives you a clue what it will behave when it is greater than length.

Similar Threads

  1. Attaching the Java documentation
    By Eranga in forum NetBeans
    Replies: 11
    Last Post: 08-09-2010, 04:17 AM
  2. SCJA Exam Voucher and Official Sun Learning CD-ROMS for Sale
    By Dutch_C0urage in forum Java Certification
    Replies: 0
    Last Post: 05-21-2009, 09:26 PM
  3. SCJA Exam Voucher and Official Sun Learning CD-ROMS for Sale
    By Dutch_C0urage in forum Reviews / Advertising
    Replies: 0
    Last Post: 05-03-2009, 02:46 PM
  4. Proofread my Java documentation?
    By javanewbie in forum New To Java
    Replies: 1
    Last Post: 06-24-2008, 08:39 AM
  5. Java Documentation
    By ravian in forum New To Java
    Replies: 4
    Last Post: 12-04-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •