0

hey i'm working on a calculator and i have a little problem here i'm trying to make my calculator able to do some operations like (9*5)+4/8/(9-5) as example i'm having some problems i thought that by stocking each value and operator in a linked list then trying to analyse the excepretion is a good way to solve that problem but i don't know what is exactly the problem i think there is an infiniteloop in my code well that is the code . T0 and t are both TextFields the same code goes for * and + and /

op ap = new op("+");
    ap.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            cal.addLast(T0.getText());
            cal.addLast("+");
            oop = '+';
            t.setText(t.getText() + oop);
            T0.setText("");

        }
    });
    ap.setBounds(256, 390, 80, 56);
    getContentPane().add(ap);

that the = button

op ae = new op("=");
    ae.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            cal.addLast(T0.getText());
            T0.setText("");
            do {
                searchpar();
            } while (cal.size() != 1);
            T0.setText((cal.get(0)));

        }
    });
    ae.setBounds(256, 445, 80, 52);
    getContentPane().add(ae);

    public void searchpar() {
    int i = 0;
    do {
        do {
            if (cal.get(i) == "(") {
                paroind = i;
                paro = true;
            }
            i++;
            if (i == cal.size())
                break;

        } while (cal.get(i) != ")");
        if (i == 0) {
            searchmuldiv(0);
            searchaddmin(0);
        }

        else {
            parfind = i;
            paro = false;
        }

        if (paro == true) {
            searchmuldiv(paroind);
            searchaddmin(paroind);
        }
    } while (i != cal.size() && cal.size() != 1);
}

public void searchmuldiv(int i) {
    do {
        while (cal.get(i) != "×" && cal.get(i) != "÷") {
            i++;
            if (i == cal.size()) {
                i--;
                break;
            }
        }
        if (cal.get(i) == "×") {

            cal.set(i - 1,
                    String.format("%2.f", Double.parseDouble(cal.get(i - 1)) * Double.parseDouble(cal.get(i + 1))));
            cal.remove(i);
            cal.remove(i + 1);
            i--;
        } else if (cal.get(i) == "÷") {
            cal.set(i - 1,
                    String.format("%2.f", Double.parseDouble(cal.get(i - 1)) / Double.parseDouble(cal.get(i + 1))));
            cal.remove(i);
            cal.remove(i + 1);
            i--;

        }

    } while (i != 1 || i != (cal.size() - 1));
}

public void searchaddmin(int i) {

    do {
        while (cal.get(i) != "+" && cal.get(i) != "-") {
            i++;
            if (i == cal.size()) {
                i--;
                break;
            }
        }

        if (cal.get(i) == "+") {

            cal.set(i - 1,
                    String.format("%2.f", Double.parseDouble(cal.get(i - 1)) + Double.parseDouble(cal.get(i + 1))));
            cal.remove(i);
            cal.remove(i + 1);
            i--;
        } else if (cal.get(i) == "-") {
            cal.set(i - 1,
                    String.format("%2.f", Double.parseDouble(cal.get(i - 1)) - Double.parseDouble(cal.get(i + 1))));
            cal.remove(i);
            cal.remove(i + 1);
            i--;

        }

    } while (i != 0 || i != (cal.size() - 1));

}

i know that code won't calculate an expression with () and so but i'm going easily trying to fix it but it deosn't even calculate a simple expression as 1+1 and just a question which one is better to use in my case string.format or string.valueOf if there is a better waay to do that calculator than the linked lists so tell me (i'm new with java )

  • If script engines are available: `new ScriptEngineManager().getEngineByName ("rhino").eval("(9*5)+4/8/(9-5)")` – Unihedron Dec 14 '17 at 21:50
  • I changed every comparison to equals but nothing changed the same infinite loop –  Dec 14 '17 at 21:54

0 Answers0