Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java String Calculator

Tags:

java

I'm trying to make a simple calculator in Java which takes input in the form of a string and does a simple '+' and '-' operation.

Single digit inputs work but my problem is when i try to implement this for double digit

input string is: 5+20+5+11
list 1 = [5, 20, 2, 0, 5, 11, 1]
list 2 = [+, +, +]
Answer:27

I need to find a way where after storing [5] in list1 how i can add [5,20] instead of [5,20,2,0] which the current code is doing.

public int calC(String input) {
        int len = input.length();
        ArrayList list1 = new ArrayList();
        ArrayList list2 = new ArrayList();
        for (int i = 0; i < len; i++) {
            if ((input.charAt(i) != '+') && (input.charAt(i) != '-')) {
                // check if the number is double-digit
                if ((i + 1 <= len - 1)) {
                    if ((input.charAt(i + 1) != '+')&& (input.charAt(i + 1) != '-')) {
                        String temp = "";
                        temp = temp + input.charAt(i) + input.charAt(i + 1);
                        int tempToInt = Integer.parseInt(temp);
                        // adding the double digit number
                        list1.add(tempToInt);
                    }
                    // add single digit number
                    list1.add(input.charAt(i) - '0');
                }
            } else {
                // adding the symbols
                list2.add(input.charAt(i));
            }
        }
        int result = 0;
        result = result + (int) list1.get(0);
        for (int t = 0; t < list2.size(); t++) {
            char oper = (char) list2.get(t);
            if (oper == '+') {
                result = result + (int) list1.get(t + 1);

            } else if (oper == '-') {
                result = result - (int) list1.get(t + 1);
            }
        }
        return result;
    }

Edit: working version
@Ker p pag thanks for the updated methods
input string is: 5+20+5+11
[5, 20, 5, 11]
[+, +, +]
Answer:41
I'll need to try to implement this with stack as suggested but the current version works

static boolean isDigit(char check) {
    if (Character.isDigit(check)) {
        return true;
    }
    return false;
}
public static int calC(String input) {

    int len = input.length();
    ArrayList list1 = new ArrayList();
    ArrayList list2 = new ArrayList();

    for (int i = 0; i < len; i++) {
        if ((i + 1 <= len - 1)) {
            if (isDigit(input.charAt(i)) && isDigit(input.charAt(i + 1))) {
                String temp = input.charAt(i) + "" + input.charAt(i + 1);
                int toInt = Integer.parseInt(temp);
                list1.add(toInt);
                i = i+1;
            } else if (isDigit(input.charAt(i))) {
                list1.add(input.charAt(i)- '0');
            } else {
                list2.add(input.charAt(i));
            }

        }
    }

    int result = 0;
    result = result + (int) list1.get(0);
    for (int t = 0; t < list2.size(); t++) {
        char oper = (char) list2.get(t);
        if (oper == '+') {
            result = result + (int) list1.get(t + 1);

        } else if (oper == '-') {
            result = result - (int) list1.get(t + 1);
        }
    }
    return result;
}
like image 266
tmax Avatar asked Dec 11 '22 04:12

tmax


2 Answers

Here is the code:

    String a = "5+20-15+8";
    System.out.println(a);
    String operators[]=a.split("[0-9]+");
    String operands[]=a.split("[+-]");
    int agregate = Integer.parseInt(operands[0]);
    for(int i=1;i<operands.length;i++){
        if(operators[i].equals("+"))
            agregate += Integer.parseInt(operands[i]);
        else 
            agregate -= Integer.parseInt(operands[i]);
    }
    System.out.println(agregate);
like image 100
HJK Avatar answered Dec 22 '22 23:12

HJK


If you want the result 41 for input string "5+20+5+11", why not use ScriptEngineManager with JavaScript engine,

public double calC(String input) {
    int result = 0;
    ScriptEngineManager mgr = new ScriptEngineManager();    
    ScriptEngine engine = mgr.getEngineByName("JavaScript");        
    return (Double)engine.eval(input);
}

But note that the return type is double here.

If you want only int as return type in this case, try with this

return new BigDecimal(engine.eval(input).toString()).intValue();
like image 28
Wundwin Born Avatar answered Dec 23 '22 01:12

Wundwin Born