Question

In: Computer Science

Android Studio (Java) I'm trying to create a simple calculator. I want to put out a...

Android Studio (Java)

I'm trying to create a simple calculator. I want to put out a message if they try to divide by 0.

I have this so far. What code should I put?

divide.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (number1.getText().length() != 0 && number2.getText().length() != 0) {
            double n1= Double.parseDouble(number1.getText().toString());
            double n2= Double.parseDouble(number2.getText().toString());

            double res= n1 / n2;

            result.setText(String.valueOf(res));
        } else {
            Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show();
        }
    }

});

Solutions

Expert Solution

Thanks for the question! Before doing the division n1 by n2, we need to ensure n2 is not equal to zero. 

We need to provide a checking just before we do that division, below is the updated code as how you need to do the checking. Please update the error message accordingly.

=============================================================================================


divide.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         if (number1.getText().length() != 0 && number2.getText().length() != 0) {
             double n1= Double.parseDouble(number1.getText().toString());
             double n2= Double.parseDouble(number2.getText().toString());

             
             // validate the denominator n2 is not zero before doing the division
             if(n2!=0){
                 double res= n1 / n2;
                 result.setText(String.valueOf(res));
             } else {
                 Toast.makeText(view.getContext(), "ERROR. INVALID OPERATION. DIVISION BY /0", Toast.LENGTH_SHORT).show();
             }
         } else {
             Toast.makeText(view.getContext(), "Please enter the numbers properly", Toast.LENGTH_SHORT).show();
         }
     }

 });

Related Solutions

I need the java code for a 4-function calculator app on android studio (do this on...
I need the java code for a 4-function calculator app on android studio (do this on android studio). Please make sure all the requirements shown below are followed (such as the error portion and etc). The topic of this app is to simply create a 4 function calculator which calculates math expressions (add, subtract, multiply, and divide numbers). The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button...
I need the java code for a 4 function calculator app on android studio (do this...
I need the java code for a 4 function calculator app on android studio (do this on android studio) - The requirements are the following : - The only buttons needed are 0-9, *, /, +, -, a clear, and enter button - Implement the onclicklistener on the main activity - The calcuator should use order of operations (PEMDAS) - It should be able to continue from a previous answer (Ex: If you type 2+6 the calculator will display 8....
I am doing a 4 function calculator app in java on android studio which calculates math...
I am doing a 4 function calculator app in java on android studio which calculates math expressions (add, subtract, multiply, and divide). I ONLY NEED THE CODE THE EQUAL BUTTON. When the user clicks the equal button, it should take the entire sequence of numbers and operators on the screen, such as 1+2*5 , and save them into a String [I am pretty sure this can be done using the toString() call]. Then, the String should be split using StringTokenizer,...
IN ANDROID STUDIO, you will create a mortgage calculator appthat allows the user to enter...
IN ANDROID STUDIO, you will create a mortgage calculator app that allows the user to enter a purchase price, down-payment amount, and an interest rate.Based on these values, the app should calculate the loan amount (purchase price minus down payment) and display the monthly payment for 10, 20, and 30-year loans.Allow the user to select a custom loan duration (in years) by using a SeekBar and display the monthly payment for that custom loan duration.Assignment deliverables (all in a ZIP...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create...
Create a Scorekeeper app in android studio by using java language - Layouts | Widgets Create the layout for your score keeping app. The app should have: Two team names (TextViews) Two scores (TextViews) Buttons to increase/ decrease the scores An amount to change the score by (RadioButtons) You must have at least two score options The scores can be changed by anything you want American football: 1, 2, 3, 6 Basketball: 1, 2, 3 Freestyle wrestling: 1, 2, 3,...
Android Studio. Java. Please create an application that -> An activity that allows user to enter...
Android Studio. Java. Please create an application that -> An activity that allows user to enter name, gender, date of birth, state of residence (selected from a pre-defined list: CA, AZ, NV, OR), email address and favorite website. This activity has a button "Show Data" that displays detail entered
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Use StringTokenizer or String.split for the calculator code.
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code below, I need the XML code that organizes the layout of the calculator (using either linearLayout or relativeLayout). As shown in the JAVA Code - Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract, buttonmultiply, buttondivide, buttonequals, buttonclear and TextView display - is what is needed in the...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *,...
On Android Studio, create a 4 function calculator. The only buttons you need are 0-9, *, /, +, -, a clear, and enter button. Having this java code, I need the XML code that organizes the layout of the calculator. JAVA CODE - package com.example.10012698.calculatorproject; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.ArrayList; import java.util.StringTokenizer; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Button button0, button1, button2, button3, button4, button5, button6, button7, button8, button9, buttonadd, buttonsubtract,...
Android Studio Code: Provide a working android studio code i.e java and xml code for the...
Android Studio Code: Provide a working android studio code i.e java and xml code for the activity below: Develop an application that is capable to turn pages back and forth. Detailed Instructions: For the main activity, create a layout that represents a fictitious title and author. The activity should include a clickable button on the bottom right that allows you to go forward to the next activity. The next activity will simply be an image above simple text that can...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT