// file: Apples.java

import java.io.*;

/** Example with different exceptions.
 * @author Jaanus Poial
 * @version 0.4
 */
public class Apples {

   /** Main and only method. */
   public static void main (String[] args) {
      int n = 0;
      int m = 0;
      while (true) {
         try {
            BufferedReader inp = new BufferedReader
                      (new InputStreamReader (System.in));
            System.out.println ("Press ctrl-C to quit");
            System.out.print ("Number of children (n): ");
            String s = inp.readLine();
            n = Integer.parseInt (s);
            if (n<0)
               throw new IllegalArgumentException ("n = " + String.valueOf (n));
            System.out.print ("Number of apples (m): ");
            s = inp.readLine();
            m = Integer.parseInt (s);
            if (m<0)
               throw new IllegalArgumentException ("m = " + String.valueOf (m));
            System.out.println ("Each child got " 
                      + String.valueOf (m/n) + " apples and "
                      + String.valueOf (m%n) + " remains");
         }
         catch (NumberFormatException e) {
            System.out.println ("Not a number: " + e.toString());
         }
         catch (ArithmeticException e) {
            System.out.println ("Caught arithmeticexception: " + e.toString());
         }
         catch (Exception other) {
            System.out.println ("Problem: " + other.toString());
         }
         finally {
            System.out.println ("n = " + n + "  m = " + m);
         }
      }
   } // main

} // Apples

// end of file
// -------------------------------------------------------------------------------------
// </pre>

