-1

it is not WORKING !!

I have used different methods to solve the case but did not work

Anyone who can help me?

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

           case '/':

                    pp = Double.parseDouble(text.getText().toString());


                    if (sss == '/') {
                        if (Double.parseDouble(text.getText().toString()) == 0.0 ||Integer.parseInt(text.getText().toString()) == 0) {

                            text.setText("");
                            text2.setText("");
                            Toast.makeText(getBaseContext(), "Cannot divide by zero", Toast.LENGTH_SHORT).show();

                        } else if (pp != 0 || pp != 0.0) {

                            vis = tt / pp;

                            temp = (int) vis;
                            if (vis == temp) {

                                text.setText(Integer.toString(temp));
                            } else {

                                text.setText(Double.toString(vis));
                            }

                            vis = 0;
                            ash = 0;
                            break;

                        }
                    }
Bigeye
  • 13
  • 2
  • 7
  • Just a hint: use **meaningful** variable names! – Phantômaxx Sep 13 '15 at 10:35
  • As a side issue, `temp = (int) vis; if (vis == temp) {} ...` comparing float and int for exact match is fragile. You should be checking if they are close enough instead. See http://stackoverflow.com/questions/1161199/is-relational-comparison-between-int-and-float-directly-possible-in-c – headuck Sep 13 '15 at 10:45

2 Answers2

2

Dividing a floating point value by zero is not an error, the result just becomes infinity. Check the result after the division:

case '/':

  pp = Double.parseDouble(text.getText().toString());


  if (sss == '/') {

    vis = tt / pp;

    if (Double.isInfinite(vis)) {
      text.setText("");
      text2.setText("");
      Toast.makeText(getBaseContext(), "Cannot divide by zero", Toast.LENGTH_SHORT).show();
    } else {

      temp = (int) vis;
      if (vis == temp) {
        text.setText(Integer.toString(temp));
      } else {
        text.setText(Double.toString(vis));
      }

      vis = 0;
      ash = 0;
      break;

    }
  }
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

Don't make your code so complex. Use just Double variable as in division you need answer to certain precision.Also there is problem with division operation with double type variables as they don't throw Arithmetic exception.E.g.:

0/0 - generates ArithmeticException
1.0/0 - generates output NaN(infinite)

It seams that "pp" is of double type. So write some thing like this:

double tt=Double.parseDouble(textView1.getText().toString().trim());
double pp=Double.parseDouble(textView2.getText().toString().trim());

if(pp!=0){
    x=tt/pp;
    System.out.println(" res :"+x);
}else{
    //you logic if pp is 0
}

Avoid computation if denominator is zero by using simple check(denominator==0) or keep as it so that when denominator is zero it shows result NaN(infinite).

VIjay J
  • 736
  • 7
  • 14