-2

here is my code.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {

        Scanner scan=new Scanner(System.in);

        String s;


        int i;
        int f=1,c,t=scan.nextInt();


        for(int j=0;j<t;j++)
            {

            s=scan.nextLine();
            scan.close();
            c=s.charAt(0)-s.charAt(1);
            for(i=2;i<s.length()-1;i=i+2)//HERE loop is upto second last element
                {
                if(s.charAt(i)-s.charAt(i+1)!=c && s.charAt(i+1)- s.charAt(i)!=c)
                    {
                    f=0;
                    break;
                }
            }

            if(f==1)
                System.out.print("Funny");
            else
                System.out.print("Not Funny");
        }
    }
}

ERROR

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.lang.String.charAt(String.java:658) at Solution.main(Solution.java:22)

I tried to search on net but I didn't get answer.

Kritner
  • 12,693
  • 10
  • 45
  • 68
  • what do you mean what is the purpose of having that error? you're attempting to access something that doesn't exist - e.g. you're if you have a `string s = "hello";` if there is no index "99", what would you expect to happen if you were to attempt to access `s[99]`? – Kritner Dec 07 '16 at 18:46
  • But I'm not getting out of it.I tried a lot but error is coming always.Can you please point out my error? – Maitreya Patel Dec 07 '16 at 18:48
  • I am not sure exactly what is happening, so forgive me, but are you sure that you are suppoed to add one to i (`s.charAt(i+1)`) in `if(s.charAt(i)-s.charAt(i+1)!=c && s.charAt(i+1)- s.charAt(i)!=c)`? .. our intervals are by two – Pythogen Dec 07 '16 at 18:53
  • yes. I want to add 1 . – Maitreya Patel Dec 07 '16 at 18:55
  • The stacktrace refers to line 22 of `Solution.java`. Which line is line 22? – Ole V.V. Dec 07 '16 at 19:11
  • In C language this type of logic will run correctly.But why not in java? In C it will compile and during running of code if we are out of rang then message will appear otherwise code will run correctly.WHY????WHY???? – Maitreya Patel Dec 07 '16 at 19:27
  • @MaitreyaPatel Have you looked at either of the proposed duplicates? – resueman Dec 07 '16 at 19:33
  • If you think your program will run correctly in C, you have a funny definitionof “correctly” :-) As far as I can tell from this thread, the behaviour in C would not be well-defined. – Ole V.V. Dec 08 '16 at 02:12

1 Answers1

1

the error message is telling you where lies the problem. That's the reason of having

error like “String index out of range” in java

The error message tells you the exception occurs in line 22, which is c=s.charAt(0)-s.charAt(1);

and it's out of bound because of String index out of range: 0, that implies when you are calling s.charAt(0), the length of s is 0, which means s is probably an empty string.

Anthony C
  • 1,782
  • 2
  • 13
  • 25