Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program performs division and does not throw an exception when you

ID: 3627888 • Letter: T

Question

The following program performs division and does not throw an exception when you input a zero for the denominator:

import java.util.Scanner;

public class Division2
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double n;
int d;

System.out.print("Enter numerator: ");
n = stdIn.nextDouble();
System.out.print("Enter divisor: ");
d = stdIn.nextDouble();
System.out.print(n / d);
}//end main
}//end Division2 class

Question: Need to have this program written so that it still employs a double numerator and int denominator, but if the value input for the denominator is zero, it refuses to perform the division operation, and keeps asking for the denominator until the user supplies something other than zero.

Explanation / Answer

import java.util.Scanner;

public class Division2
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
double n;
int d = 0;

System.out.print("Enter numerator: ");
n = stdIn.nextDouble();

System.out.print("Enter divisor: ");
d = stdIn.nextInteger();
while ( d == 0 ) // been doing too much visual basic forgot the double equals in original post
{

System.out.print("Enter a non-zero divisor: ");
d = stdIn.nextInteger();
}
System.out.print(n / d);
}//end main
}//end Division2 class