aspose file tools
The moose likes Java in General and the fly likes doubt with string predefined  method Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "doubt with string predefined  method" Watch "doubt with string predefined  method" New topic
Author

doubt with string predefined method

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 973

public class SB
{
public static void main(String args[])
{
StringBuffer s1=new StringBuffer();
s1.insert(1,"Hello from Java");
System.out.println(s1);
}

}

why is it giving an exception Stringindexoutofbounds, i havent even specified the range or capacity???


http://www.lifesbizzare.blogspot.com || OCJP:81%
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

You try to insert a String just before the second character (or just after the first character). But there are no characters, so it tries to insert the String at a location that does not exist. Remember, insertions etc work on the actual contents, not the capacity.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: doubt with string predefined method
 
Similar Threads
Please explain me the output in detail
StringBuffer var as an argument
String buffer
StringBuffer question
I thought it should be "Hello" instead of "Hello how are you"