King123321
King123321

Reputation: 3

Divide by Zero Simple Program

 public static void main(String [] args) {

 Scanner userInput = new Scanner(System.in);

 int myCents = 0;
 int myDollars = 0;
 int myQuarters = 0;
 int myDimes = 0;
 int myNickels = 0;
 int myPennies = 0;

 System.out.printf("Please enter the amount of cents below: \n");
 myCents = userInput.nextInt();

 myDollars = myCents/100;
 myQuarters = myCents % myDollars;
 myDimes = myCents % myQuarters;
 myNickels = myCents % myDimes;
 myPennies = myCents % myNickels;

 System.out.printf(myCents + " is equal to %4d", myDollars);
 System.out.printf(" dollar(s), " + myQuarters);
 System.out.printf(" quarter(s), " + myDimes);
 System.out.printf(" dime(s), " + myNickels);
 System.out.printf(" nickel(s) and " + myPennies + " pennies. \n");
}
}
Error:  java.lang.ArithmeticException: / by zero

The error seems to keep popping up. We have not learned if statements for Java as of yet. It is supposed to be a simple program.

Upvotes: 0

Views: 91

Answers (5)

Taylor Buchanan
Taylor Buchanan

Reputation: 4757

% is the modulus operator. A modulo operation requires first performing division. In Java, like many other programming languages, an error is thrown when an integer is divided by zero.

This is the first line that could possibly cause the error, most likely due to zero being input by the user.

myQuarters = myCents % myDollars;

Upvotes: 0

Marvin
Marvin

Reputation: 14255

someNumber % 0 will result in a java.lang.ArithmeticException. This can happen in several places of your code. E.g. if the user enters a number below 100 then myDollars will be "0" or if the user enters "100" then myDollars will be "1" and myQuarters will be "0".

As a general advice: Unless you know how to properly debug your code (which I assume you haven't learned yet), you can always use System.out.println (or System.out.printf) to inspect the values of your variables at a certain point.

In your example you could e.g. move your System.out.printf statements right after the calculation of the values to see which value is exactly "0" when the exception is thrown.

Upvotes: 0

Taher A. Ghaleb
Taher A. Ghaleb

Reputation: 5240

I think, you should convert your variables to double:

public static void main(String [] args) {

 Scanner userInput = new Scanner(System.in);

 double myCents = 0;
 double myDollars = 0;
 double myQuarters = 0;
 double myDimes = 0;
 double myNickels = 0;
 double myPennies = 0;

 System.out.printf("Please enter the amount of cents below: \n");
 myCents = userInput.nextInt();

 myDollars = myCents/100;
 myQuarters = myCents % myDollars;
 myDimes = myCents % myQuarters;
 myNickels = myCents % myDimes;
 myPennies = myCents % myNickels;

 System.out.printf(myCents + " is equal to %4f", myDollars);
 System.out.printf(" dollar(s), " + myQuarters);
 System.out.printf(" quarter(s), " + myDimes);
 System.out.printf(" dime(s), " + myNickels);
 System.out.printf(" nickel(s) and " + myPennies + " pennies. \n");
}

This should resolve the issue.

Upvotes: 0

RudolphEst
RudolphEst

Reputation: 1250

Any of myDollars, myDimes, myNickels or myPennies could be 0 due to the nature of the modulus operator(%). Then you use that value to do more modulus operations, which effectively divide by 0 before returning the remainder.

You should add if statements.

Upvotes: 0

George
George

Reputation: 915

myCents/100 is integer divison and so if myCents < 100, myDollars will be 0. Therefire % myDollars will throw your / by zero exception.

Instead try:

myDollars = myCents / 100;
myCents = myCents % 100;
myQuarters = myCents / 25;
myCents = myCents%25;

etc. (assuming a quarter is 25 cents... I've never used USD)

Upvotes: 3

Related Questions