#java.lang.ArithmeticException: / by zero

25 messages · Page 1 of 1 (latest)

quasi zealot
#

Method call in main() is exactChange(userIn, coinValues) with userIn simply being scnr.nextInt() and int[] coinValues being an array with values {1, 5, 10, 25}
(literally nothing else of importance is in main() aside from those 3 things mentioned)


        int numQuarters = userTotal / coinVals[3];

        int numDimes = (userTotal - (coinVals[3] * numQuarters)) / coinVals[2];

        int numNickels = ((userTotal - (coinVals[3] * numQuarters)) - coinVals[2] * numDimes) / coinVals[1];

        int numPennies = (((userTotal - (coinVals[3] * numQuarters)) - coinVals[2] * numDimes) - coinVals[1] * numNickels) / coinVals[0];

        if (numPennies > 1) {System.out.println(numPennies + " pennies"); }
        else if (numPennies == 1) {System.out.println(numPennies + " penny"); }

        if (numNickels > 1) { System.out.println(numNickels + " nickels"); }
        else if (numNickels == 1) { System.out.println(numNickels + " nickel"); }

        if (numDimes > 1) { System.out.println(numDimes + " dimes"); }
        else if (numDimes == 1) { System.out.println(numDimes + " dime"); }

        if (numQuarters > 1) { System.out.println(numQuarters + " quarters"); }
        else if (numQuarters == 1) { System.out.println(numQuarters + " quarter"); }

    } ```
Typed this up, while yes for homework, and ran it just fine on VSC, but upon throwing it to the auto-grader it threw the division by 0 error even though none of the array values, which are mainly used as the denominators, are 0. So any possible ideas why it could be throwing a / by zero exception? Not asking for a "do this" answer, though any pointers or nudges would be helpful and appreciated cause I think I've rewritten the calculations like 3 times.
random latchBOT
#

This post has been reserved for your question.

Hey @quasi zealot! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

strong jewel
#

well, what line is it erroring on

#

(also use modulo for cleaner code)

#

also is the exactChange method specified by the assignment? if so, it may be passing in its own coinVals for testing

random latchBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.

quasi zealot
#

Afraid cant give specific line as yes it passes its own thing, example being exactChange(300, coinVals), and it doesnt give a specific line, just throws the error

strong jewel
#

does the assignment maybe require you to check the array passed?

quasi zealot
#

I do not think so, though maybe could work. All it specifies regarding the coinVals array is that index 0 = 1 (pennies), 1 = 5 (nickels), 2 = 10 (dimes) and 3 = 25 (quarters)

strong jewel
#

could you show the problem statemennt

quasi zealot
#

Like the specific instruction? Assuming so, sure gimme a sec to boot pc, just woke up

strong jewel
#

good morning

quasi zealot
#

heres the instructions plus example i/o

#

and heres the 2 calls it tries to do and then errors on

strong jewel
#

looks like you're misunderstanding what exactChange does

#

read the problem statement again

#

separate out which parts are the inputs and outputs, make sure to not confuse what the main method does vs what the exactChange method does

#

hint: ||coinVals is an output, not an input||

quasi zealot
strong jewel
#

right it's not supposed to be just that

quasi zealot
#

THONK imma ask directly and ask "whats it supposed to look like then"
and finally realized coinValues was supposed to be an empty array, which led to getting the 10/10 finally

#
        userTotal %= 25;

        int numDimes = userTotal / 10;
        userTotal %= 10;

        int numNickels = userTotal / 5;
        userTotal %= 5;```
and as requested heres the modulo
quasi zealot
random latchBOT