What is StringIndexOutOfBoundsException?

StringIndexOutOfBoundsException is an unchecked exception. It occurs when you try to access a character from a String at an index greater than the length of the String.

Below is a demo program for StringIndexOutOfBoundsException:

public class StringIndexOutOfBoundsExceptionDemo {

	public static void main(String[] args) {
		String s = "ashok";
		char charAt = s.charAt(100); 


Below is the output when you run the above program:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 100
	at java.lang.String.charAt(String.java:686)
	at StringIndexOutOfBoundsExceptionDemo.main(StringIndexOutOfBoundsExceptionDemo.java:6)

How to run a Java Program with code written in Notepad

Create a temporary folder C:\mywork(Suppose).  Using Notepad or another text editor, create a small Java file HelloWorld.java with the following text:

Step 1: 

      public class HelloWorld { 
            public static void main(String[] args) { 
                  System.out.println("Hello, World!"); 

Save your file as HelloWorld.java in C:\mywork. 

Run Command Prompt (found under All Programs/Accessories in the Start menu).

Step 2: 

     Type         C:\> cd \mywork
     This makes C:\mywork the current directory. C:\mywork> dir This displays the directory contents.  You should see HelloWorld.java among the files.

Step 3: 

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin
     This tells the system where to find JDK programs. 

Step 4:

C:\mywork> javac HelloWorld.java 
    This runs javac.exe, the compiler.  You should see nothing but the next system prompt...

Step 5:

C:\mywork> dir
     javac has created the HelloWorld.class file.  You should see HelloWorld.java and HelloWorld.class among the files.