Kyle Joseph Green
Kyle Joseph Green

Reputation: 129

Divide by Zero Exception with Typecaste (Java)

import java.util.Scanner;


public class SumDigits {

public static void main(String[] args)
{
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    // prompt the user to enter a value and store it as the integer called number
    System.out.print("Enter an integer: ");
    double number = Math.abs(input.nextDouble());

    System.out.println("The sum of the digits is " + sumNumbers(number));

    input.close();
}
public static int sumNumbers (double number)
{

    int sum = 0;

    for (int i = 10, digit = 0; (number * 10) /i !=0; i *= 10, digit = (int)((number % i) - digit)/(i / 10))
    {
        sum += digit;
    }

    return sum;
}
}

At runtime, I get the error message

Exception in thread "main" java.lang.ArithmeticException: / by zero

referring to line 25 (my for loop conditions).

The loop worked fine until I tried type casting digit's value to an int, and I'm not really certain why that would cause any part of the loop to divide something by zero. I've gone over all the possibilities regarding the conditions that use rational expressions and can't deduce a contingency wherein any denominator would be set to zero. I get this error regardless of what number is input. I would not have chosen to save number as a double at all if it were not for the fact that my professor provided a number whose value exceeds that which can be stored within an int in one of his test cases. The program ran fine prior to the type cast and provided the correct answer for all other test cases.

Upvotes: 3

Views: 531

Answers (3)

Neeraj Gupta
Neeraj Gupta

Reputation: 811

Here you are doing

(number * 10) /i !=0

where number is double.

Floating point numbers aren't recommended for comparisons because of the way they get stored (in mantissa and exponent form). So, if this condition returns true, consider yourself lucky.

Because of that your loop is kinda never-ending loop. The reason you are getting arithmetic exception though is that here you are multiplying i by 10 in this kinda infinite loop. "i" reaches "integer" limit of 32 bits and overflows and ultimately makes all those 32 bits as 0.

Effectively, i=0 and both of the following throw divide by zero exception

(number * 10) /i 

and

(number % i) - digit)/(i / 10)

Upvotes: 1

akhil_mittal
akhil_mittal

Reputation: 24157

First time control enters into loop the value of digit would be zero and not the last digit of entered number as you would have expected. Also you need to fix your loop termination condition as that is wrong. For example if number is 123 then at last expected iteration 1230/1000 = 1 and not 0 and loop does not end and rather leads to overflow. You can use the following:

public static int sumNumbers (double number)
    {
        int sum = 0;
        for (int i = 10, digit = (int)(number % i); digit != 0; i *= 10, digit = (int)((number % i) - digit)/(i / 10))
        {
            sum += digit;
        }

        return sum;
    }

This should work but I again recommend you to improve this code as this is not the ideal way.

Testing:

Enter an integer: 3456 The sum of the digits is 18

Upvotes: 0

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

The root cause of the ArithmeticException is the incorrect loop condition. Adding System.out.println("i=" + i + " i*10=" + (i * 10)); in the loop produces output:

i=10 i*10=100
i=100 i*10=1000
i=1000 i*10=10000
i=10000 i*10=100000
i=100000 i*10=1000000
i=1000000 i*10=10000000
i=10000000 i*10=100000000
i=100000000 i*10=1000000000
i=1000000000 i*10=1410065408
i=1410065408 i*10=1215752192
i=1215752192 i*10=-727379968
i=-727379968 i*10=1316134912
i=1316134912 i*10=276447232
i=276447232 i*10=-1530494976
i=-1530494976 i*10=1874919424
i=1874919424 i*10=1569325056
i=1569325056 i*10=-1486618624
i=-1486618624 i*10=-1981284352
i=-1981284352 i*10=1661992960
i=1661992960 i*10=-559939584
i=-559939584 i*10=-1304428544
i=-1304428544 i*10=-159383552
i=-159383552 i*10=-1593835520
i=-1593835520 i*10=1241513984
i=1241513984 i*10=-469762048
i=-469762048 i*10=-402653184
i=-402653184 i*10=268435456
i=268435456 i*10=-1610612736
i=-1610612736 i*10=1073741824
i=1073741824 i*10=-2147483648
i=-2147483648 i*10=0

Upvotes: 0

Related Questions