Connecting Tech Pros Worldwide Help | Site Map

Loops

momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#1: Jun 13 '08
how this line of code can know that there is more input needed?:

Expand|Select|Wrap|Line Numbers
  1. boolean continueLoop = true;
the original code is :

Expand|Select|Wrap|Line Numbers
  1. public class DivideByZeroWithExceptionHandling
  2. {
  3.    // demonstrates throwing an exception when a divide-by-zero occurs
  4.    public static int quotient( int numerator, int denominator )
  5.       throws ArithmeticException
  6.    {
  7.       return numerator / denominator; // possible division by zero
  8.    } // end method quotient
  9.  
  10.    public static void main( String args[] )
  11.    {
  12.       Scanner scanner = new Scanner( System.in ); // scanner for input
  13.       boolean continueLoop = true; // determines if more input is needed
  14.  
  15.       do
  16.       {
  17.          try // read two numbers and calculate quotient
  18.          {
  19.             System.out.print( "Please enter an integer numerator: " );
  20.             int numerator = scanner.nextInt();
  21.             System.out.print( "Please enter an integer denominator: " );
  22.             int denominator = scanner.nextInt();
  23.  
  24.             int result = quotient( numerator, denominator );
  25.             System.out.printf( "\nResult: %d / %d = %d\n", numerator,
  26.                denominator, result );
  27.             continueLoop = false; // input successful; end looping
  28.          } // end try
  29.          catch ( InputMismatchException inputMismatchException )
  30.          {
  31.             System.err.printf( "\nException: %s\n",
  32.                inputMismatchException );
  33.             scanner.nextLine(); // discard input so user can try again
  34.             System.out.println(
  35.                "You must enter integers. Please try again.\n" );
  36.          } // end catch
  37.          catch ( ArithmeticException arithmeticException )
  38.          {
  39.             System.err.printf( "\nException: %s\n", arithmeticException );
  40.             System.out.println(
  41.                "Zero is an invalid denominator. Please try again.\n" );
  42.          } // end catch
  43.       } while ( continueLoop ); // end do...while
  44.    } // end main
  45. } // end class DivideByZeroWithExceptionHandling
momotaro's Avatar
Needs Regular Fix
 
Join Date: Sep 2006
Posts: 259
#2: Jun 13 '08

re: Loops


ok sorry I figure it out thank you anyway :) !
Reply