Hi, its me again. I need help one last time. This time my problem is pretty simple, I just can't find it myself it seems. Please lend some assistance.

Here's my assignment.

Create a program that takes in 2 integers and displays the result of dividing the two integers. This program should prompt for two integers and display the results repeatedly until the user types in -99.

The main method calls another method. The other method contains the prompt and the division. The main method has a try and catch that surrounds the call to the second method. The catch is for a “divide by zero error” (you have to figure out which one that is). When the error is caught, display a message.

import java.util.InputMismatchException;
import java.util.Scanner;

public class DivideByZero
{
	public static int quotient( int numerator, int denominator )
		throws ArithmeticException
	{
		return numerator / denominator;
	}

	public static void main( String args[] )
	{
		Scanner scanner = new Scanner(System.in );
		boolean continueLoop = true;
		
		do
		{
			try
			{
				System.out.print( "Please enter an integer numerator: ");
				int numerator = scanner.nextInt();
				System.out.print( "Please enter an integer denominator: ");
				int denominator = scanner.nextInt();
				
				if ( numerator != -99)
				{
				int result = quotient( numerator, denominator );
				System.out.printf( "\nResult: %d / %d = %d\n", numerator,
					denominator, result );
					continueLoop = false;
				}
				else
				{
				 continueLoop = true;
				}
			}
			
			catch ( InputMismatchException inputMismatchException )
			{
				System.err.printf ( "\nException: %s\n",
					inputMismatchException );
				scanner.nextLine();
				System.out.println( "You must enter integers.  Please try again. \n" );
			}
			catch ( ArithmeticException arithmeticException )
			{
				System.err.printf( "\nException: %s\n", arithmeticException );
				System.out.println( "Zero is an invalid denominator.  Please try again." );
			}
		}while ( continueLoop );
	}
}

My problem is that my program will not terminate after inputting -99. Any help, you guys? Thank you in advance.

Recommended Answers

All 2 Replies

You just have the boolean values reversed in the conditions

if ( numerator != -99) {
                    int result = quotient( numerator, denominator );
                    System.out.printf( "\nResult: %d / %d = %d\n", numerator,
                      denominator, result );
                    //continueLoop = true;   don't even need this one
                } else {
                    continueLoop = false;  // set to false if -99
                }
commented: Thanks again! +1

Nevermind this, thank you for your help. It is always a simple solution, mostly anyways.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.