Ad

Own String Parser Java Which Solves Mathematical Equations Wrote Down In A String

- 1 answer

I've wrote a method/function in Java which returns the result of a given basic equation. This equation will be given as a String and I think I got this method working but don't know why I need this one line of Code because this should work without it. After trying for more than an hour to solve it I gave up and hope you can give me an aswer.

Here the Code:

    public static double format(String s) {

    char[] c = s.toCharArray();

    if(s.contains("(")) {
        int openbrackets = 0;
        for (int i = 0; i < s.length() - 2; i++) {
            if (c[i] == '(') openbrackets++;
            else if (c[i] == ')') {
                openbrackets--;
                if(openbrackets == 0) {
                    s = s.replace(s.substring(s.indexOf('('), i+1), ""+(format(s.substring(s.indexOf('(')+1, i))));
                    break;
                }
            }
        }
    }
    if (s.contains("(")) {    // String can still contains brackets
        s = "" + format(s);
    }

    c = s.toCharArray();
    for(int i = c.length-1; i >= 0; i--) {
        if(c[i] == '+') {
            return format(s.substring(0, i)) + format(s.substring(i+1, s.length()));
        } else if(c[i] == '-') {
            return format(s.substring(0, i)) - format(s.substring(i+1, s.length()));
        }
    }

    for(int i = s.length()-1; i > 0; i--) {
        if(c[i] == '*') {
            return format(s.substring(0, i)) * Double.parseDouble(s.substring(i+1, s.length()));
        } else if (c[i] == '/') {
            return format(s.substring(0, i)) / Double.parseDouble(s.substring(i+1, s.length()));
        }
    }

    return s.equals("") ? 0 : Double.parseDouble(s); // I don't understand why I need to do this line
}

Description: I don't know why I need this s.equals("") ? : because the String never should be empty however when I run it with this equation ((23)+(23-23-432-35-1-2-4231+2312+12323-(-3))*3/2) for example I get an error without it. I need the parser to convert config Strings into Numbers for example when it comes to screenresolution. I know I can also use Libraries but I want to try these things by myself.

PS: Dont hate me just because I don't use libraries. I really tried to figure it out and I have fun doing it. I would just like to know why I have to write this little Codeline as I don't figure it out...

Edit: Error was a NumberFormatException as the Parsing got an empty String... Got my error now also the OverflowException which was mentioned in the comments...

EDIT: To everyone who MIGHT use something like this in the future: Here the Code which actually works:

    public static double format(String s) {
    s = s.replace(" ", "");
    s = s.replace("\t", "");
    char[] c = s.toCharArray();
    if(s.contains("(")) {
        int openbrackets = 0;
        for (int i = 0; i < s.length(); i++) {
            if (c[i] == '(') openbrackets++;
            else if (c[i] == ')') {
                openbrackets--;
                if(openbrackets == 0) {
                    s = s.replace(s.substring(s.indexOf('('), i+1), ""+(format(s.substring(s.indexOf('(')+1, i))));
                    break;
                }
            }
        }
    }
    if (s.contains("(")) s = "" + format(s);
    c = s.toCharArray();
    for(int i = c.length-1; i > 0; i--) {
        if(c[i] == '+') {
            return format(s.substring(0, i)) + format(s.substring(i+1, s.length()));
        } else if(c[i] == '-') {
            return format(s.substring(0, i)) - format(s.substring(i+1, s.length()));
        }
    }
    for(int i = s.length()-1; i > 0; i--) {
        if(c[i] == '*') {
            return format(s.substring(0, i)) * Double.parseDouble(s.substring(i+1, s.length()));
        } else if (c[i] == '/') {
            return format(s.substring(0, i)) / Double.parseDouble(s.substring(i+1, s.length()));
        }
    }
    return s.equals("") ? 0 : Double.parseDouble(s);
}
Ad

Answer

I'm fairly sure this is at least one location in your code where you pass a 0 length string to your format function:

c = s.toCharArray();
for(int i = c.length-1; i >= 0; i--) {
    if(c[i] == '+') {
        return format(s.substring(0, i)) + format(s.substring(i+1, s.length()));
    } else if(c[i] == '-') {
        return format(s.substring(0, i)) - format(s.substring(i+1, s.length()));
    }
}

Your loop counter in (int i = c.length-1; i >= 0; i--) will get decremented until it is 0 in value if there are no + or - values in the input string. Then you call format(s.substring(0, i)) where i = 0 so I think this is one place where you will be passing a zero length/empty string to your function.

Please use a debugger and step through your code - not only would it teach you a valuable skill it would also probably give you the answer you're looking for.

Ad
source: stackoverflow.com
Ad