• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

substring() errata / OCA Java SE 8 Study Guide

 
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


On page 108, in reference to the code above, the text reads

"The request is for the characters starting at index 3 until you get to index 3."

This is incorrect.

The first parameter is the starting index.  The second parameter is the starting index plus the number of characters to include in the substring.  Thus we get an empty string (no characters) when we ask for

Further proof that this is logically correct is the fact that the length of the substring (as defined by the length() method) can be calculated by subtracting the starting index from number of characters it comprises.

This code demonstrates the concept:

 
Dwight Webber
Greenhorn
Posts: 16
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dwight Webber wrote:

Further proof that this is logically correct is the fact that the length of the substring (as defined by the length() method) can be calculated by subtracting the starting index from the number of characters it comprises.



I made a mistake here.  The sentence should read

Further proof that this is logically correct is the fact that the length of the substring (as defined by the length() method) can be calculated by subtracting the starting index from the number of characters it comprises plus the starting index.
 
author & internet detective
Posts: 41907
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think the text is wrong. The JavaDoc defines the parameters as:

beginIndex - the beginning index, inclusive.
endIndex - the ending index, exclusive.



I think your way of describing it is factually correct. I think the approach the JavaDoc and our book uses is easier to understand.

 
reply
    Bookmark Topic Watch Topic
  • New Topic