0

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);
                          }
                      }
                    }
                }
            
        }
        }
        

    
    
Amit
  • 1
  • 2
  • @HenryTwist but i am just trying to take one character and not a substring why would this happen for just one character? – Amit Apr 09 '21 at 00:57
  • 1
    As the answer in the post I linked says: "it is thrown to indicate that an index of some sort (such as to an array, to a string, or to a vector) is out of range.e.g using List.". So your index that you're using is out of bounds of the size of the string. – Henry Twist Apr 09 '21 at 01:00
  • How can it go out of bounds? I am running a loop from 0 to the last character index of the string? @HenryTwist – Amit Apr 09 '21 at 01:03
  • 1
    You're actually running it until the character *after* the last, which doesn't exist. I would recommend checking out this (old but still great) article on [how to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). If you put the effort in to debug you can find the problem yourself most of the time. – Henry Twist Apr 09 '21 at 01:05
  • Your issue is this that you are using `<=` instead of `<` on this line `for (j=0;j<=fin.length();j++)`, you should use only `<` like this `for (j=0;j – sorifiend Apr 09 '21 at 04:21

1 Answers1

0
  1. What is the sense of below lines of code? Can you please tell me what are the values of m and n?
if (m > n || m < 100 || n > 9999) {
            System.out.println("INVALID INPUT");
}
  1. You just need to update below line
for (j=0;j<=fin.length();j++)
Ksh
  • 36
  • 2