public
Created

This is from exercise 13-2 (using dates) in Murach's Java Programming 4. I have the program written but the age comes out incorrect. If you use my birthday Feb 17, 1982 it gives an age of 662. I checked the math by hand and it should give the correct result of 30. If you look at the calculateAge() function I have outputs for each step of the calculation, it only fails on the last step (converting from days to years).

  • Download Gist
AgeCalculatorApp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import java.util.*;
import java.text.DateFormat;
 
public class AgeCalculatorApp
{
private static Date today = new Date();
private static GregorianCalendar gregBirthDate = new GregorianCalendar();
private static Date birthDate = new Date();
private static DateFormat formattedDate = DateFormat.getDateInstance(DateFormat.SHORT);
public static void main(String[] args)
{
//Get the user's birthdate
System.out.println("Welcome to the age calculator.");
Scanner sc = new Scanner(System.in);
int birthMonth = Validator.getInt(sc, "Enter the month you were born (1 to 12): ",
0, 13) - 1;
int birthDay = Validator.getInt(sc, "Enter the day of the month you were born: ",
0, 32);
int birthYear = Validator.getInt(sc, "Enter the year you were born (four digits): ",
1874, 2012);
long age = calculateAge(birthMonth, birthDay, birthYear);
String todaysDate = formattedDate.format(today);
String formattedBDay = formattedDate.format(birthDate);
System.out.println("Your birth date is: " + formattedBDay);
System.out.println("Today's date is: " + todaysDate);
System.out.println("Your age is: " + age);
 
}
public static long calculateAge(int birthMonth, int birthDay, int birthYear){
long age = 0;
gregBirthDate.set(birthYear, birthMonth, birthDay);
birthDate = gregBirthDate.getTime();
long birthDateMS = birthDate.getTime();
long todayMS = today.getTime();
long elapsedMS = todayMS - birthDateMS;
age = elapsedMS / (1000 * 60 * 60 * 24 * 365);
//checking the math one step at a time
System.out.println(elapsedMS);
System.out.println(elapsedMS / 1000);
System.out.println(elapsedMS /(1000 * 60));
System.out.println(elapsedMS /(1000 * 60 * 60));
System.out.println(elapsedMS /(1000 * 60 * 60 * 24));
System.out.println(elapsedMS /(1000 * 60 * 60 * 24 * 365));
return age;
}
}
Validator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
import java.util.Scanner;
 
public class Validator
{
public static String getString(Scanner sc, String prompt)
{
System.out.print(prompt);
String s = sc.next(); // read the first string on the line
sc.nextLine(); // discard any other data entered on the line
return s;
}
 
public static String getLine(Scanner sc, String prompt)
{
System.out.print(prompt);
String s = sc.nextLine(); // read the whole line
return s;
}
 
public static int getInt(Scanner sc, String prompt)
{
boolean isValid = false;
int i = 0;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextInt())
{
i = sc.nextInt();
isValid = true;
}
else
{
System.out.println("Error! Invalid integer value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return i;
}
 
public static int getInt(Scanner sc, String prompt,
int min, int max)
{
int i = 0;
boolean isValid = false;
while (isValid == false)
{
i = getInt(sc, prompt);
if (i <= min)
System.out.println(
"Error! Number must be greater than " + min);
else if (i >= max)
System.out.println(
"Error! Number must be less than " + max);
else
isValid = true;
}
return i;
}
 
public static double getDouble(Scanner sc, String prompt)
{
boolean isValid = false;
double d = 0;
while (isValid == false)
{
System.out.print(prompt);
if (sc.hasNextDouble())
{
d = sc.nextDouble();
isValid = true;
}
else
{
System.out.println("Error! Invalid decimal value. Try again.");
}
sc.nextLine(); // discard any other data entered on the line
}
return d;
}
 
public static double getDouble(Scanner sc, String prompt,
double min, double max)
{
double d = 0;
boolean isValid = false;
while (isValid == false)
{
d = getDouble(sc, prompt);
if (d <= min)
System.out.println(
"Error! Number must be greater than " + min);
else if (d >= max)
System.out.println(
"Error! Number must be less than " + max);
else
isValid = true;
}
return d;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.