0

I'm trying to figure to figure out where I can be more efficient in my code, rather than having repeating IF statements. It's currently taking my NB IDE 25 seconds to run my basic Calculator app.

I'd appreciate any help, as I can only find code snippets but not an actual method of where I should first look to cut down.

My current code for my equals function is:

private void jBtn18ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String finalAnswer;

        secondnumber = Double.parseDouble(jtxtDisplay.getText());


                if (operations == "+")
                {
                    result = firstnumber + secondnumber;
            String answer = String.format("%.0f",result);
                            jtxtDisplay.setText(answer);
                }
                else if (operations == "-")
                {
                    result = firstnumber - secondnumber;
            String answer = String.format("%.0f",result);
                            jtxtDisplay.setText(answer);
                }
                else if (operations == "/")
                {
                    result = firstnumber / secondnumber;
            String answer = String.format("%.0f",result);
                            jtxtDisplay.setText(answer);
                }
                else if (operations == "*")
                {
                    result = firstnumber * secondnumber;
            String answer = String.format("%.0f",result);
                            jtxtDisplay.setText(answer);
                }
                else if (operations == "%")
                {
                    result = firstnumber % secondnumber;
            String answer = String.format("%.0f",result);
                            jtxtDisplay.setText(answer);
                }
    }      
PMmSL
  • 21
  • 1
  • 3
    I suspect this doesn't work at all, that isn't [how you compare `String`(s) in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java). – Elliott Frisch Sep 11 '16 at 17:19
  • It's very unlikely that the shown code causes the application to be slow. Use a performance analysis tool to find out why your code is slow. See here https://blog.idrsolutions.com/2014/06/java-performance-tuning-tools/ for a selection of tools – Guenther Sep 11 '16 at 18:33

2 Answers2

0

There isn't much that you can shorten. You gotta keep the 5 ifs. Also, it's not correct to compare strings like that.

if (operations.equals("+"))
{
    result = firstnumber + secondnumber;
}
else if (operations.equals("-"))
{
    result = firstnumber - secondnumber;
}
else if (operations.equals("/"))
{
    result = firstnumber / secondnumber;
}
else if (operations.equals("*"))
{
    result = firstnumber * secondnumber;
}
else if (operations.equals("%"))
{
    result = firstnumber % secondnumber;
}
String answer = String.format("%.0f",result);
jtxtDisplay.setText(answer);

Explanation:

== compares the memory locations of the strings which might be different for strings that have the same characters. .equals compares the actual characters of the string, so it should be used.

These two lines of code:

String answer = String.format("%.0f",result);
jtxtDisplay.setText(answer);

are repeated in every if statement. It should be pulled out.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    With Java 7 and up it is possible to use switch on strings. – Mark Rotteveel Sep 11 '16 at 17:26
  • @MarkRotteveel Yeah I know that. But using a switch won't shorten the code (in terms of number of lines). Just think about the `break` statements that have to be written. – Sweeper Sep 11 '16 at 17:29
  • Well - just tongue in cheek - with the brace-style shown it will save one line per condition :). And if you make a helper method with a return per case, it will save another line. – Mark Rotteveel Sep 11 '16 at 17:36
0

You can out the repeated 2 Lines at the end. And of course you have to usw equals method to Compressor strings.

private void jBtn18ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        String finalAnswer;

        secondnumber = Double.parseDouble(jtxtDisplay.getText());




       if (operations.equals("+"))
        {
            result = firstnumber + secondnumber;

        }
        else if (operations.equals("-"))
        {
            result = firstnumber - secondnumber;

        }
        else if (operations.equals("/"))
        {
            result = firstnumber / secondnumber;

        }
        else if (operations.equals("*"))
        {
            result = firstnumber * secondnumber;

        }
        else if (operations.equals("%"))
        {
            result = firstnumber % secondnumber;
        }

        String answer = String.format("%.0f",result);
        jtxtDisplay.setText(answer);
}    

Other possiblity is to use case instead of "switch ... case"

Jens
  • 67,715
  • 15
  • 98
  • 113