// BirthdaysGregorianCalendar.java (c) 2005 Kari Laitinen // http://www.naturalprogramming.com // 2004-08-19 Java file created. // 2005-06-20 Last modification. // By default, the Scanner object referenced by keyboard // uses whitespace characters (e.g. spaces and line termination // characters) as delimiters where data input operations stop. // In this program we need to use the - character as a delimiter // when the user types in the date in the format YYYY-MM-DD. // The regular expression [-\\s] specifies the delimiters which // the nextInt() methdod recognizes when it is reading the data // from the keyboard. [-\\s] specifies two delimiters. - means that // the hyphen sign is a delimiter. \\s is interpreted after compilation // as \s and it means any whitespace character. import java.util.* ; class BirthdaysGregorianCalendar { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; System.out.print( "\n Type in your date of birth as YYYY-MM-DD" + "\n Please, use four digits for the year: " ) ; keyboard.useDelimiter( "[-\\s]" ) ; int year_of_birth = keyboard.nextInt() ; int month_of_birth = keyboard.nextInt() ; int day_of_birth = keyboard.nextInt() ; Calendar date_of_birth = new GregorianCalendar( year_of_birth, month_of_birth - 1, day_of_birth ) ; System.out.printf( "\n You were born on a %tA", date_of_birth ) ; System.out.print( "\n Here are your days to celebrate. You are\n" ) ; int years_to_celebrate = 10 ; while ( years_to_celebrate < 80 ) { Calendar date_to_celebrate = new GregorianCalendar( year_of_birth + years_to_celebrate, month_of_birth - 1, day_of_birth ) ; System.out.printf( "\n %1$d years old on %2$tF (%2$tA)", years_to_celebrate, date_to_celebrate ) ; years_to_celebrate = years_to_celebrate + 10 ; } } } /****** First I wrote the beginning of this program in the following way: String date_of_birth_as_string = keyboard.nextLine() ; Scanner text_to_scan = new Scanner( date_of_birth_as_string ) ; text_to_scan.useDelimiter( "-" ) ; int year_of_birth = text_to_scan.nextInt() ; int month_of_birth = text_to_scan.nextInt() ; int day_of_birth = text_to_scan.nextInt() ; *****/