public int detectEqOrder(String equation, char x) {
        int result = 0;

        int start = -1;
        int order = 0;

        for (int i = 0; i < equation.length(); i++) {
            if (equation.charAt(i) == x) {
                start = i;
            }

            if (start != -1 && i < equation.length() - 1) {
                if (equation.charAt(start + 1) != '^') {
                    order = 1;
                } else {
                    if (i < equation.length() - 2) {
                        int end = start + 2;
                        while(isInt(equation.charAt(end))) {
                            end++;
                            if (end == equation.length())
                                break;
                        }

                        order = Integer.parseInt(
                                equation.substring(start + 2, end)
                        );

                        i = end;
                        start = -1;
                    }
                }
            }

            if (order > result) {
                result = order;
            }
        }

        return result;
    }