Skip to main content

trying to get the calculator to return 0 not an error message when a number is divided by 0?



Here is problem in a nut shell. I have made a basic caluclator in java but I am trying to getting the calculator to return 0 when a number is entered and it is divided by 0. Each time I keep getting an error message!







class Calculator

{



long sum;



public void evaluate( char action, long number )

{

if (action == '+')

{

sum = number + number;



}



if (action == '-')

{

sum = number - number;



}



if (action == '*')

{

sum = number * number;



}



if (action == '/')

{

sum = number / number;



}



**if (sum == number / 0)

{

sum = 0;

}

}**




Comments

  1. I'd just add this logic to your division code:

    if (action == '/')
    {
    if (number == 0)
    {
    sum = 0;
    } else {
    sum = number / number;
    }
    }


    Also, why are you using the same number per operation? The subtraction operator will always return 0. If you are using this code in a calculator, just check whether the denominator is 0.

    ReplyDelete
  2. if (action == '/')
    {
    if(number2 == 0)
    {
    System.out.println("You cannot divide by 0!");
    return;
    }
    sum = number1 / number2;
    }

    ReplyDelete
  3. With ternary operator:

    if (action == '/') {
    sum = number == 0 ? 0 : number / number; // <-- isn't that just 1 ?
    }

    ReplyDelete
  4. The IEEE 754 standard includes not only positive and negative numbers that consist of a sign and magnitude, but also positive and negative zeros, positive and negative infinities, and special Not-a-Number values (hereafter abbreviated NaN). A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.

    You need to add condition make sure you are not dividing by ZERO.

    if(secondnumber == 0)
    {

    return;
    }

    ReplyDelete
  5. Just for the sake of completeness, why not catch the exception (and use java coding conventions) ?

    if (action == '/') {
    try {
    sum = number / number;
    catch (ArithmeticException e) {
    //log error
    sum = 0;
    }
    }


    The bonus here is that it handles whatever else wrong could happen.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.