Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I ran my code, but I am getting error, I don't understand what the error is. How should I fix it?

+6 votes
asked Jul 18 by npatel300 (1,440 points)
//A -> I = E

//E -> P O P | P

//O -> + | - | * | / | **

//P -> I | L | UI | UL | (E)

//U -> + | - | !

//I -> C | CI

//C -> a | b | ... | y | z

//L -> D | DL

//D -> 0 | 1 | ... | 8 | 9

public class Main

{

public static void main(String[] args)

{

    s = args.length == 1 ? args[0] : "";

    if (A() && i == s.length())

    {

        System.out.println("The string \"" + s + "\" is in the language.");

    }

    else{

        System.out.println("The string \"" + s + "\" is not in the language.");

    }

}

private static boolean A()

{

    if(I())

    {

        return true;

    }

    else if(E())

    {

        return true;

    }

    else if (A()){

        return true;

    }

    return false;

}

    private static boolean E()

    {

    if(P())

    {

        return true;

    }

    if (O()){

        return true;

    }

    if (P()){

        return true;

    }

    else if (P()){

        return true;

    }

    return false;

}

    private static boolean O()

    {

        if (s.charAt (i) == '+' || s.charAt (i) == '-' )

        {

            i++;

            if (O()){

                return true;

            }

        }

        if (s.charAt (i) == '*' || s.charAt (i) == '/' || s.charAt (i) == '*' && s.charAt (i) == '*')

        {

            i++;

            if (O()){

                return true;

            }

        }

        return false;

    }

    

    private static boolean P()

    {

        if (I()){

            return true;

        }

        else if (L()){

            return true;

        }

        if (s.length() == 'U' && s.length() == 'I'){

            return true;

        }

        else if (s.length() == 'U' && s.length() == 'L'){

            return true;

        }

        else if (i < s.length() && s.charAt(i) == '('){

            ++i;

        if (E()) {

            if (i < s.length() && s.charAt(i) == ')') {

                ++i;

            return true;

            }

        }

    }

        return false;

    }

    

    private static boolean U()

    {

        if (i < s.length() && (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '!'))

        {

            i++;

            if (U()){

                return true;

            }

        }

        return false;

    }

    

    private static boolean I()

    {

        if (C()){

            return true;

        }

        else if (s.length() == 'C' && s.length() == 'I'){

            return true;

        }

        return false;

    }

    

    private static boolean C()

    {

        if (i < s.length() && 'a' <= s.charAt(i) && s.charAt(i) <= 'z'){

            i++;

            return true;

        }

        else if (i < s.length() && 'b' <= s.charAt(i) && s.charAt(i) <= 'y'){

            i++;

            return true;

        }

        return false;

    }

    

    private static boolean L()

    {

        if (D()){

            return true;

        }

        else if (s.length() == 'D' && s.length() == 'L'){

            return true;

        }

        return false;

    }

    

    private static boolean D()

    {

        if (i < s.length() && '0' <= s.charAt(i) && s.charAt(i) <= '9'){

            i++;

            return true;

        }

        else if (i < s.length() && '1' <= s.charAt(i) && s.charAt(i) <= '8'){

            i++;

            return  true;

        }

        return false;

    }

    

    private static String s;

    private static int i;

    }

1 Answer

+1 vote
answered Jul 19 by Peter Minarik (34,450 points)

The error is the following:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.String.charAt(String.java:658)
        at Main.O(Main.java:113)
        at Main.E(Main.java:87)
        at Main.A(Main.java:57)
        at Main.main(Main.java:29)

Let's look at it closely at the exception message.

​You're indexing a string with an invalid index. This could be a negative number, or longer or equal to the actual number of characters stored in the string.

Let's see the call stack.

You can see that your call originated from the bottom to the top: main(), A(), E(), O() then the exception was actually thrown in String.charAt(). So you look at your own code, what your last call was and investigate there:

        at Main.O(Main.java:113)

So look at the code:

        if (s.charAt (i) == '+' || s.charAt (i) == '-' )

What could be possibly wrong here? Well, if your original input is empty (""), then you're trying to access the 0th character, which does not exist, hence the exception is raised.

How do I know this? Well, I looked at your code and quickly deduced that in the main you set s to empty string (unless your program has arguments specified).

Then you can verify the theory by going to the line 113, and add some logging to see if you're indeed trying to access an invalid index:

System.out.println("i: " + i);

I provided some valid arguments: "three=1+2", and now there is no exception on line 113. However, your code does not work correctly, as the statement should be part of the language, but your program says it is not.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...