Question

I am making a calculator app in which I wanna show an alert is user divides anything by zero.REST all works fine but i user divides anything by zero an alert should b raised

package com.example.calculator;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Calci extends Activity {
    TextView t1;
    EditText e1, e2;
    Button add, sub, mul, div;
    Context c = this;

    String b, a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calci);
        e1 = (EditText) findViewById(R.id.EditText01);
        e2 = (EditText) findViewById(R.id.EditText02);
        add = (Button) findViewById(R.id.add);
        sub = (Button) findViewById(R.id.sub);
        mul = (Button) findViewById(R.id.mul);
        div = (Button) findViewById(R.id.div);
        t1 = (TextView) findViewById(R.id.textView1);
        div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                AlertDialog.Builder a1 = new AlertDialog.Builder(c);
                try{
                if (a.equals("") || b.equals("")) {

                    // AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }
                }
                catch (ArithmeticException e) {
                Log.d("ERROR", "msg");
                }

//              if (b.equals(0)) {
//                  a1.setTitle("INVALID");
//                  a1.setMessage("CAnnot divide by zero");
//                  a1.setPositiveButton("YES",
//                          new DialogInterface.OnClickListener() {
//                              public void onClick(DialogInterface dialog,
//                                      int button1) {
//                                  // if this button is clicked, close
//                                  // current activity
//                                  dialog.cancel();
//                              }
//
//                          });
//                  AlertDialog alertDialog = a1.create();
//                  a1.show();



                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) / Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);


            }

        });

        mul.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) * Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) - Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                a = e1.getText().toString();
                b = e2.getText().toString();
                if (a.equals("") || b.equals("")) {

                    AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                    // Setting Dialog Title
                    a1.setTitle("Alert Dialog");

                    // Setting Dialog Message
                    a1.setMessage("PLEASE ENTER SOMETHING");

                    a1.setPositiveButton("yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int button1) {
                                    // if this button is clicked, close
                                    // current activity
                                    dialog.cancel();
                                }

                            });

                    // Showing Alert Message
                    AlertDialog alertDialog = a1.create();
                    a1.show();

                }

                else {
                    Log.d("sadasd", "msg");
                    int result = Integer.parseInt(a) + Integer.parseInt(b);
                    t1.setText(Integer.toString(result));
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(add.getWindowToken(),
                            InputMethodManager.RESULT_UNCHANGED_SHOWN);
                }

            }

        });

    }
}

I am making a calculator app in which I wanna show an alert is user divides anything by zero.

Was it helpful?

Solution

Simple, Just add one more condition

div.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a = e1.getText().toString();
                b = e2.getText().toString();
                AlertDialog.Builder a1 = new AlertDialog.Builder(c);
                try{
                if (a.equals("") || b.equals("") || b.equals("0")) {

                 ........
                .........

or else you can do it like this

if (a.equals("") || b.equals("") {
     .......
     .......
}else if (b.equals("0")){
              AlertDialog.Builder a1 = new AlertDialog.Builder(calci.this);
              a1.setTitle("INVALID");
              a1.setMessage("CAnnot divide by zero");
              a1.setPositiveButton("YES",
                      new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog,
                                  int button1) {
                              // if this button is clicked, close
                              // current activity
                              dialog.cancel();
                          }

                      });
              AlertDialog alertDialog = a1.create();
              a1.show();
}else{
 ....  
 ....  
}

OTHER TIPS

make a check in Div button onClick() function.

String divd = edittext.getText().toString();
if(divd.equals("0")){
Toast.makeText(ActivityName.this, "You are dividing by 0", Toast.LENGTH_LONG).show();
return;
}
 a = e1.getText().toString();
            b = e2.getText().toString();
            AlertDialog.Builder a1 = new AlertDialog.Builder(c);
            try{
            if  b.equals("0")) {

                // AlertDialog.Builder a1 = new AlertDialog.Builder(c);

                // Setting Dialog Title
                a1.setTitle("Alert Dialog");

                // Setting Dialog Message
                a1.setMessage("You are dividing by 0");

                a1.setPositiveButton("OK",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int button1) {
                                // if this button is clicked, close
                                // current activity
                                dialog.cancel();
                            }

                        });

                // Showing Alert Message
                AlertDialog alertDialog = a1.create();
                a1.show();

            }
            }
            catch (ArithmeticException e) {
            Log.d("ERROR", "msg");
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top