Guest User

Untitled

a guest
Feb 4th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Scanner;
  4. import org.joda.time.DateTime;
  5. public class ageInMonths
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         System.out.println("Please enter your birth month.");
  10.         boolean isNumeric = false;
  11.         //array order is: month, year
  12.         int birth[] = new int[2];
  13.         Scanner kb = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  14.         while (!isNumeric)
  15.         {
  16.             try
  17.             {
  18.                 birth[0] = kb.nextInt();
  19.                 if (birth[0] == 0)
  20.                 {
  21.                     throw new Exception();
  22.                 }
  23.                 isNumeric = true;
  24.             }
  25.             catch (Exception e)
  26.             {
  27.                 System.out.println("Please enter in an integer not zero.");
  28.                 kb.nextLine();
  29.             }
  30.         }
  31.         isNumeric = false;
  32.         System.out.println("Please enter your birth year.");
  33.         while (!isNumeric)
  34.         {
  35.             try
  36.             {
  37.                 birth[1] = kb.nextInt();
  38.                 isNumeric = true;
  39.             }
  40.             catch (Exception e)
  41.             {
  42.                 System.out.println("Please enter in an integer.");
  43.                 kb.nextLine();
  44.             }
  45.         }
  46.         int old[] = new int[2];
  47.         DateTime dt = new DateTime();
  48.         old[0] = dt.getMonthOfYear() - birth[0];
  49.         old[1] = dt.getYear() - birth[1];
  50.         old = conversion(old);
  51.         System.out.println("You are " + (old[0] + old[1]*12) + " months old.");
  52.     }
  53.     static int[] conversion(int[] old)
  54.     {
  55.         if (old[0] < 0)
  56.         {
  57.             old[0] = old[0] + 12;
  58.             --old[1];
  59.         }
  60.         if (old[0] > 11)
  61.         {
  62.             old[0] = old[0] - 12;
  63.             ++old[1];
  64.         }
  65.         return conversion(old);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment