Simple Calculator Project (Java - Android Studio)

Hello, I have a question about the extra points I can work on this project https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/developing-an-android-application/modules/building-android-apps/informationals/simple-calculator-project

There are three extra tasks I can work with:

  • Adding multiplication and division operators.

    • You’ll need to add two more radio buttons and additional else if statements to check if division or multiplication are selected.
  • Ensuring the app doesn’t crash when the equals button is pressed and one of the number boxes is empty.

    • When equals is pressed, before you do anything, ensure that the number boxes aren’t empty so an error doesn’t throw.
  • Allowing the user to input decimal numbers.

    • You’ll need to use a decimal text box and store the numbers as double s instead of int s.

For the first two (multiplication and division operators, and preventing the app crush), I have coded MainActivity.javalike below but it gave me an error:

package com.codecademy.simplecalculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText firstNumber = findViewById(R.id.number1);
        final EditText secondNumber = findViewById(R.id.number2);
        final RadioGroup operators = findViewById(R.id.operators);
        final RadioButton add = findViewById(R.id.add);
        final RadioButton subtract = findViewById(R.id.subtract);
        final Button equals = findViewById(R.id.equals);
        final TextView result = findViewById(R.id.results);

        equals.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 final int firstNumberValue = Integer.parseInt(firstNumber.getText().toString());
                 final int secondNumberValue = Integer.parseInt(secondNumber.getText().toString());

                 final int operatorButtonId = operators.getCheckedRadioButtonId();

                 Integer answer;

                 if(operatorButtonId == add.getId()) {
                     answer = firstNumberValue + secondNumberValue;
                 } else if {
                     answer = firstNumberValue - secondNumberValue;
                 } else if {
                    answer = firstNumberValue * secondNumberValue;
                 } else {
                    answer = firstNumberValue / secondNumberValue;
                }

                if(equals.getText().toString() == ""){
                    // do something
                }else{
                    // do something else
                }

                 result.setText(answer.toString());
            }
        });

    }
}

For the decimal number task, I assume I can do something like the below code. I didn’t save and operate that decimal number so I am not sure if it fires correctly:

double input = 3.14159265359;
        System.out.println("double : " + input);
        System.out.println("double : " + String.format("%.2f", input));
        System.out.format("double : %.2f", input);

I appreciate your tips on how I can fix this java code.

Could you post the error, please?

1 Like

Hi @codeneutrino , here it is:

/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:40: error: '(' expected
                 } else if {
                          ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:40: error: illegal start of expression
                 } else if {
                           ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:40: error: ')' expected
                 } else if {
                            ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:41: error: illegal start of expression
                     answer = firstNumberValue - secondNumberValue;
                            ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:41: error: ';' expected
                     answer = firstNumberValue - secondNumberValue;
                                              ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:41: error: variable declaration not allowed here
                     answer = firstNumberValue - secondNumberValue;
                              ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:41: error: not a statement
                     answer = firstNumberValue - secondNumberValue;
                                                 ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:42: error: illegal start of type
                 } else if {
                   ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:42: error: ';' expected
                 } else if {
                       ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:44: error: illegal start of type
                 } else {
                   ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:44: error: ';' expected
                 } else {
                       ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:45: error: <identifier> expected
                    answer = firstNumberValue / secondNumberValue;
                          ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:46: error: ')' expected
                }
                 ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:48: error: not a statement
                if(equals.getText().toString() == ""){
                  ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:48: error: ';' expected
                if(equals.getText().toString() == ""){
                                                     ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:50: error: 'else' without 'if'
                }else{
                 ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:56: error: class, interface, or enum expected
        });
         ^
/Users/mamikawamura/AndroidStudioProjects/SimpleCalculator/app/src/main/java/com/codecademy/simplecalculator/MainActivity.java:58: error: class, interface, or enum expected
    }
    ^

else if need a condition after them, just like if statements. That means the syntax looks like this:

if (condition1) {

} else if (condition2) {
//notice there is still a condition with else if
} else {
//notice else does not have a condition
}

Do your else if blocks have conditions?

else if

doesn’t have any condition, so I fully understood why it didn’t fire correctly.

I’m getting stuck with adding multiply/division. I created two Radiogroups - one for add/subtract and another for multiple/division in the design mode at activity_Main.

For MainActivity.Java, shall I create equals.setOnClickListener(new View.OnClickListener() { @Override for these two RadioGroups? Here is what I am doing:

@Override
            public void onClick(View v) {
                final int firstNumberValue = Integer.parseInt(firstNumber.getText().toString());
                final int secondNumberValue = Integer.parseInt(secondNumber.getText().toString());

                final int operatorButtonId = operators.getCheckedRadioButtonId();

                Integer answer;

                if(operatorButtonId == add.getId()) {
                    answer = firstNumberValue + secondNumberValue;
                } else {
                    answer = firstNumberValue - secondNumberValue;
                }


                result.setText(answer.toString());
            }
        });

@Override
            public void onClick(View v) {
                final int firstNumberValue = Integer.parseInt(firstNumber.getText().toString());
                final int secondNumberValue = Integer.parseInt(secondNumber.getText().toString());

                final int operatorButtonId = operators.getCheckedRadioButtonId();

                Integer answer;

                if(operatorButtonId == multiply.getId()) {
                    answer = firstNumberValue * secondNumberValue;
                } else {
                    answer = firstNumberValue / secondNumberValue;
                }


                result.setText(answer.toString());
            }
        });

Not sure if this is the correct solution for having four buttons (multiply, add, divide, subtract).

Sorry I haven’t gotten back to you. At a glance it seems to be fine; is there anything going wrong with it?