0

I have placed below my problem is as simplified as possible. Given a string containing a literal mathematical expression I want to get the same expression by converting letters to numbers taking values from a map. I posted below my code for any help from you guys.

public class Prova {
    public static void main(String[] argv) {
        //mathematical expression
        String expr = "(a*b)+(b/a)";  //(15*60)+(60/15)
        //Map of numeric values
        HashMap<String, Double> itemval = new HashMap();

        itemval.put("a", 15.0);
        itemval.put("b", 60.0);

        System.out.println("itemval: " + itemval);
        System.out.println("expr: " + expr);

        //convert literal expression numerically
        char[] nc = expr.toCharArray();
        for (int t = 0; t < nc.length; t++) {
            char pippo = nc[t];
            for (String key : itemval.keySet()) {
                if (key.equals(pippo)) {
                    System.out.println("pippo: " + itemval.get(key));
                }
            }
            System.out.print(nc[t]);
        }
    }
}

what I want is to get an output like (15 * 60) + (60/15) Thanks in advance...

ootklm
  • 1
  • 2
  • If you are sure that your "a" will always be single char and will not appear in other places (so no thing like (a*bab)), simply use String.replaceAll() – Nadir Jun 21 '16 at 06:54
  • This might help you: `http://stackoverflow.com/questions/7258538/free-java-library-for-evaluating-math-expressions` – Kartic Jun 21 '16 at 07:01

2 Answers2

0

Your could work with String instead of char arrays to do the replacement.

/* expr.split("") create an arrays of string. 
 * Each String element is a character of the original string.    
 */
for (String s : expr.split("")) { 
    if (itemval.containsKey(s)) {
        expr = expr.replaceAll(s, itemval.get(s).toString());
    }
}
System.out.println(expr);

Output: (15.0*60.0)+(60.0/15.0)


If you want to format the number to don't show the .0, you can use a NumberFormat:

NumberFormat nf = NumberFormat.getNumberInstance();
for (String s : expr.split("")) {
    if (itemval.containsKey(s)) {
        expr = expr.replaceAll(s, nf.format(itemval.get(s)));
    }
}

Output: (15*60)+(60/15)

David SN
  • 3,389
  • 1
  • 17
  • 21
0
import java.util.HashMap;
import java.util.Map;

public class Prova {
   public static void main(String[] argv) {
        // mathematical expression
        String expr = "(a*b)+(b/a)"; // (15*60)+(60/15)
        // Map of numeric values
        HashMap<String, Double> itemval = new HashMap();

        itemval.put("a", 15.0);
        itemval.put("b", 60.0);

        System.out.println("itemval: " + itemval);
        System.out.println("expr: " + expr);

        for (Map.Entry<String, Double> e : itemval.entrySet()) {
            expr = expr.replaceAll(e.getKey(), String.valueOf(e.getValue()));
        }

        System.out.println(expr);
    }
}

Output:

itemval: {b=60.0, a=15.0}
expr: (a*b)+(b/a)
(15.0*60.0)+(60.0/15.0)