I'm new in java and need help for this method.
An integer is said to be self-divisible if it is divisible by the sum of its digits. For example, both 12 and -12 are self-divisible, since 12 is divisible by 3, which is the sum of the digits 1 and 2. Write a method public boolean isSelfDivisible(int a) that checks whether the parameter a is self-divisible.
Paweł Baczyński wrote:Of course. I was just wondering whether this method should return false or throw ArithmeticException.
well then...perhaps you should have asked that question.
I mean, when i enter -12, it needs java arithmetic.
Avoid such sentences until you have strong evidence about your solution corretness
ashkan ahmadi wrote:This is my final code.
How your program works with negative values?
=ashkan ahmadi wrote:For example, both 12 and -12 are self-divisible
Why you taking user's input inside the method, which supposed to check only if number is self divisible?
ashkan ahmadi wrote:When i input negative values, it highlighted line 16 and says in the box that
java.lang.arithmeticexception: /bye zero
It's because in case a number is negative, the control will never enter the while loop and sum will remain 0.
You may want to replace num > 0 with num != 0.