java.lang.StringIndexOutOfBoundsException-reverse of a number example in Java

In this Program i am going to do reverse of a number in Java.

Error Message:

Given alphabet is:uLowercase
Given alphabet is:rLowercase
Given alphabet is:yLowercase
Given alphabet is:aLowercase
Given alphabet is: Lowercase
Given alphabet is:IUppercase
Given alphabet is:nLowercase
Given alphabet is:fLowercase
Given alphabet is:oLowercase
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10
       at java.lang.String.charAt(String.java:658)
       at com.lib.CheckCaseOfaCharacter.main(CheckCaseOfaCharacter.java:11)



Error Description:

In below red background forloop if you assign index variable as 1,it will prints from index(1) and index variable lessthan or equalto we get an error.Because String length starts from 0.

package com.lib;

public class CheckCaseOfaCharacter
{
       public static void main(String[] args)
       {
              String alphabets = "Surya Info";
              int length = alphabets.length();
              for(int w=1;w<=length;w++)
              {
                     char character = alphabets.charAt(w);
                     if(Character.isUpperCase (character))
                     {
                           System.out.println("Given alphabet is:"+alphabets.charAt(w)+ "Uppercase");
                     }
                     else
                     {
                           System.out.println("Given alphabet is:"+alphabets.charAt(w)+"Lowercase");
                     }
              }
             
             
       }

}

Error Solution:

package com.lib;

public class CheckCaseOfaCharacter
{
       public static void main(String[] args)
       {
              String alphabets = "Surya Info";
              int length = alphabets.length();
              for(int w=0;w<length;w++)
              {
                     char character = alphabets.charAt(w);
                     if(Character.isUpperCase (character))
                     {
                           System.out.println("Given alphabet is:"+alphabets.charAt(w)+ "Uppercase");
                     }
                     else
                     {
                           System.out.println("Given alphabet is:"+alphabets.charAt(w)+"Lowercase");
                     }
              }
             
             
       }

}



Output:

Given alphabet is:SUppercase
Given alphabet is:uLowercase
Given alphabet is:rLowercase
Given alphabet is:yLowercase
Given alphabet is:aLowercase
Given alphabet is: Lowercase
Given alphabet is:IUppercase
Given alphabet is:nLowercase
Given alphabet is:fLowercase
Given alphabet is:oLowercase
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