Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

StringBuilder.delete() help!!!

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


the code above does compile and run.




this second code, compile and error in runtime. StringIndexOutOfBoundException.

Why the first code does not give runtime error? It seems there is extra one index in StringBuffer object.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The API documentation answers your question:

Throws:
StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.


In your second case, start is greater than length(), so it throws StringIndexOutOfBoundsException.
[ August 16, 2007: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

the API says about StringBuffer's delete(int start,int end) method:


Throws:
StringIndexOutOfBoundsException - if start is negative, greater than length(), or greater than end.


This makes sense, because there is always a null string at the end of a character sequence, so the 3 is ok, 4 is too high.
More about this (yet a different question) on
http://faq.javaranch.com/java/ScjpFaq#kb-regexp
(have a look on the image there)

Note, that delete() does not throw an exception, if the "end" number is too high.


Yours,
Bu.
 
Fedry Kemilau
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

Now i got it already.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic