public String solveExpression( String expression ) { boolean done = false; // The current string keeps our working version of the answer. String current = expression; // The current and eventually final numerical answer. int result = 0; // The index of the next operator we have to consider. Default is 0 which will mean we // haven't found a real operator since all real operators need arguments on both sides of them. int minimumIndex = -1; int m = -1; int l = -1; int r = -1; // while we're not done solving... while ( !done ) { m = -1; if ( current.contains( "*" ) || current.contains( "/" ) || current.contains( "%" ) ){ // get the index in the string of the first occurence of these operators since java will do them // from left to right int multiplication = current.indexOf("*"); int division = current.indexOf("/"); int modulas = current.indexOf("%"); // if there's a valid index for mult then pick it else take a valid index for div else take one for mod. m = (multiplication != -1) ? multiplication : ( (division != -1) ? division : ( (modulas != -1) ? modulas : -1 ) ) ; // Let's make sure we have a valid index before going further... if (m != -1) { // One of the indices is valid so let's make sure it's the first if (modulas > -1 && multiplication > -1 && division > -1) { if ( modulas < division && modulas < multiplication ) { m = modulas; } else if ( division < multiplication ) { m = division; } else { m = multiplication; } } else if ( division > -1 && multiplication > -1 ) { if ( multiplication < division ) { m = multiplication; } else { m = division; } } else if ( modulas > -1 && multiplication > -1 ) { if ( multiplication < modulas ) { m = multiplication; } else { m = modulas; } } else if ( modulas > -1 && division > -1 ) { if ( division < modulas ) { m = division; } else { m = modulas; } } // Now perform arithmetic and check to make sure we're not dividing or mod'ing by 0 if ( current.charAt( m ) == '*' ) { l = getNumberAt(current, m - 1); r = getNumberAt(current, m + 1); result = l * r; } else if ( current.charAt( m ) == '/' ) { l = getNumberAt(current, m - 1); r = getNumberAt(current, m + 1); // since divide by 0 is normally an error condition we're going to return // in the middle of the method if (r == 0) { return "undefined"; } result = l / r; } else if ( current.charAt( m ) == '%' ) { l = getNumberAt(current, m - 1); r = getNumberAt(current, m + 1); // since divide/mod by 0 is normally an error condition we're going to return // in the middle of the method if (r == 0) { return "undefined"; } result = l % r; } } } else if (current.contains( "+" ) || current.contains( "-" )){ // next precedence int a = current.indexOf("+"); int b = getFirstSubtractionIndex(current); m = a; if (a > -1 && b > -1 ) { if ( a < b ) m = a; else a = b; } else if ( b > -1 ) { m = b; } if (m != -1) { if ( current.charAt( m ) == '+' ) { l = getNumberAt(current, m - 1); r = getNumberAt(current, m + 1); result = l + r; } else if ( current.charAt( m ) == '-' ) { l = getNumberAt(current, m - 1); r = getNumberAt(current, m + 1); result = l - r; } } } minimumIndex = m; int leftSide = l; int rightSide = r; if (minimumIndex >= 1) { int rightSize = new Integer(rightSide).toString().length(); int leftSize = new Integer(leftSide).toString().length(); if (minimumIndex-leftSize != 0 && minimumIndex+rightSize!=current.length()-1) { current = current.substring(0, minimumIndex- leftSize ) + result + current.substring(minimumIndex + rightSize+1, current.length()); } else if (minimumIndex-leftSize != 0 ) { current = current.substring(0, minimumIndex-leftSize) + result ; } else if (minimumIndex+rightSize!=current.length()-1) { current = result + current.substring(minimumIndex +rightSize+1, current.length()); } else { current = result + ""; } } if (current.length() <= 1 || minimumIndex == -1 ) { done = true; } minimumIndex = -1; } return ""+result; }