1

I'm trying to make a calculator GUI in Java, however I can't figure out how to convert "7/7" into a double.

I've tried converting it directly into a double, but I can't get it to work.

private class handler implements ActionListener{

    String answerString = "";
    int answerDouble;

    public void actionPerformed(ActionEvent e) {

        if(e.getSource() == clear) {
            answerString = "";
            result.setText(answerString);
        }
        if(e.getSource() == openBracket) {
            answerString = answerString + "(";
            result.setText(answerString);
        }
        if(e.getSource() == closeBracket) {
            answerString = answerString + ")";
            result.setText(answerString);
        }
        if(e.getSource() == divide) {
            answerString = answerString + "/";
            result.setText(answerString);
        }
        if(e.getSource() == seven) {
            answerString = answerString + "7";
            result.setText(answerString);
        }
        if(e.getSource() == equal) {
            answerDouble = Integer.parseInt(answerString);
            answerString = Double.toString(answerDouble);
            result.setText(answerString);
        }
    }

}

It should display 1 for 7/7 but instead outputs an error on the command line.

Tom
  • 16,842
  • 17
  • 45
  • 54

1 Answers1

0

You can use JavaScript ScriptEngine to evaluate such expressions:

String expression = "7/7";
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("JavaScript");
System.out.println(scriptEngine.eval(expression)); // you will have to handle ScriptException

Output:

1
Mushif Ali Nawaz
  • 3,707
  • 3
  • 18
  • 31
  • I think this is a large leap of faith as the OP is asking for a Java based solution. – Bwvolleyball Sep 24 '19 at 17:55
  • 2
    @Bwvolleyball That is Java. – Max Vollmer Sep 24 '19 at 17:57
  • @Bwvolleyball Well, this is also Java-based solution. For evaluating the expression I've used `JavaScript` engine. – Mushif Ali Nawaz Sep 24 '19 at 17:57
  • 1
    I understand, but it also dodges the original issue - the original issue is that OP wasn't handling the fact that the operation symbols needed to be dealt with, so while this is a valid solution, it hides the fact that under the covers the javascript engine is dealing with the division / multiplication / etc. in a way similar to how the OP initially started attempting to solve it. The primary issue is that it's a 'cute' solution, but doesn't explain to the OP what was incorrect in the first place. – Bwvolleyball Sep 24 '19 at 18:00
  • 2
    Not sure I understand your point. OP needs to evaluate a mathematical expression, this is a simple and valid solution. Who cares that the underlying engine is hiding what it's doing. 99% of writing clean code in Java is having clutter and unnecessary detail hidden away, so developers can concentrate on writing business logic, instead of reinventing wheels. – Max Vollmer Sep 24 '19 at 18:06
  • @MaxVollmer Absolutely correct! – Mushif Ali Nawaz Sep 24 '19 at 18:06
  • Not arguing with those points, simply trying to explain the root issue to the OP. It's equally important to understand all the fancy tricks you employ, otherwise it's useless to have them in the first place. ¯\\_(ツ)_/¯ – Bwvolleyball Sep 24 '19 at 18:34
  • 1
    I know it's been a while, but thank you, Sir. Sorry for the late reply I was very new to stack overflow back then. – Jake Watson Apr 14 '21 at 15:48