java.lang.StringIndexOutOfBoundsException example in Java


In this Program i am going to do Display Vowels in Java.
     
Error Message:

Enter a Alphabet
hai
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
       at java.lang.String.charAt(String.java:658)
       at com.lib.SwitchVowels.main(SwitchVowels.java:12)


Error Description:

In below red background line i gave charAt value as 5,but i gave input as hai in this 5th character is not there,so I got an error.

package com.lib;

import java.util.Scanner;

public class SwitchVowels {

       public static void main(String[] args)
       {
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter a Alphabet");
              String alpha = sc.nextLine();
              char ch = alpha.charAt(5);
              switch(ch)
              {
                     case 'a':
                     System.out.println(ch+"is Vowel");
                     break;
                    
                     case 'e':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'i':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'o':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'u':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'A':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'E':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'I':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'O':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'U':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     default:
                           System.out.println(ch+"is Consonents");

              }
             
       }

}



Error Solution:

package com.lib;

import java.util.Scanner;

public class SwitchVowels {

       public static void main(String[] args)
       {
              Scanner sc = new Scanner(System.in);
              System.out.println("Enter a Alphabet");
              String alpha = sc.nextLine();
              char ch = alpha.charAt(0);
              switch(ch)
              {
                     case 'a':
                     System.out.println(ch+"is Vowel");
                     break;
                    
                     case 'e':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'i':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'o':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'u':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'A':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'E':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'I':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'O':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     case 'U':
                           System.out.println(ch+"is Vowel");
                           break;
                          
                     default:
                           System.out.println(ch+"is Consonents");

              }
             
       }

}


Output:

Enter a Alphabet
u
uis Vowel


Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar