java.lang.StringIndexOutOfBoundsException: String index out of range: 9

I am getting the same error even after trying out other solutions on this forum can you please check it this is the line that is giving the error

if (fin!=null && fin.length()>0 && fin.charAt(j)== ('1'))

error

java.lang.StringIndexOutOfBoundsException: String index out of range: 9 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at FascinatingNumber.main(FascinatingNumber.java:40)
anyways here is the full code

    import java.io.*;
public class FascinatingNumber
{
    public static void main(String Args[])throws IOException
    {
        DataInputStream in=new DataInputStream(System.in);
        System.out.println("Enter upper limit and lower limit");
        int m=Integer.parseInt(in.readLine());
        int n=Integer.parseInt(in.readLine());
        int a1=0;
        int a2=0;
        int a3=0;
        int a4=0;
        int a5=0;
        int a6=0;
        int a7=0;
        int a8=0;
        int a9=0;
        int i=0;
        int j=0;
                    if (m>n || m<100 || n>9999)
        {
            System.out.println("INVALID INPUT");
        
        }
        else
            {
                
                for(i=m ;i<=n; i++)
                    {
                     int num=i;
                     int num1=i*2;
                     int num2=i*3;
                     String s1=Integer.toString(num);
                     String s2=Integer.toString(num1);
                     String s3=Integer.toString(num2);
                     String fin= s1+s2+s3;
                     for (j=0;j<=fin.length();j++)
                      {
                        if (fin!=null && fin.length()>0 && fin.charAt(j)== ('1'))
                         {
                             a1=a1+1;
                         }
                         else 
                         {
                             a1=a1+0;
                            }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('2'))
                         {
                             a2=a2+1;
                         }
                         else 
                         {
                             a2=a2+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('3'))
                         {
                             a3=a3+1;
                         }
                         else 
                         {
                             a3=a3+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('4'))
                         {
                             a4=a4+1;
                         }
                         else 
                         {
                             a4=a4+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('5'))
                         {
                             a5=a5+1;
                         }
                         else 
                         {
                             a5=a5+0;
                         }   
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('6'))
                         {
                             a6=a6+1;
                         }
                         else 
                         {
                             a6=a6+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('7'))
                         {
                             a7=a7+1;
                         }
                         else 
                         {
                             a7=a7+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('8'))
                         {
                             a8=a8+1;
                         }
                         else 
                         {
                             a8=a8+0;
                         }
                         if (fin!=null && fin.length()>0 && fin.charAt(j)==('9'))
                         {
                             a9=a9+1;
                         }
                         else 
                         {
                             a9=a9+0;
                         }
                         if(a1==1 && a2==1 && a3==1 && a4==1 && a5==1 && a6==1 && a7==1 && a8==1 && a9==1)
                          {
                              System.out.println("Fascinating number is "+ i);
                          }
                      }
                    }
                }
            
        }
        }
        

    
    


Read more here: https://stackoverflow.com/questions/67013637/java-lang-stringindexoutofboundsexception-string-index-out-of-range-9

Content Attribution

This content was originally published by amit at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: