Using If/Else - Defaults to first variable and doesn't go further...
Code:
import java.io.*;
import java.util.*;
import java.text.*;
public class NewHappyBirthday {
public static void main(String[] args) throws IOException
{
String srgName, srgYear, srgMonth, srgDay;
int iYearBorn, iMonthBorn, iDayBorn, iCalendarNowDayOfYear;
BufferedReader dataIn= new BufferedReader(new InputStreamReader(System.in));
System.out.println ("Hi! Please enter your name.");
String Name = dataIn.readLine();
System.out.println("Please enter the year (yyyy) you were born.");
srgYear = dataIn.readLine();
iYearBorn = Integer.parseInt(srgYear);
System.out.println("Please enter the month (mm) you were born.");
srgMonth = dataIn.readLine();
iMonthBorn = Integer.parseInt(srgMonth);
System.out.println("Please enter the date (dd) you were born.");
srgDay = dataIn.readLine();
iDayBorn = Integer.parseInt(srgDay);
//create calendar called calendarNow and set it to the current time and date
GregorianCalendar calendarNow = new GregorianCalendar();
//long longNow = dateNow.getTime();
int iCalendarBdayDayOfYear, iCalendarNowYear, iCalendarBdayYear;
//create calendar called calendarBday and set it to the user's birthday
//(iMonthBorn-1 because Jan starts at 0)
GregorianCalendar calendarBday = new GregorianCalendar(iYearBorn, iMonthBorn-1, iDayBorn);
Date dateBday = calendarBday.getTime();
//long longBday = dateBday.getTime();
//create date format called df and output user's name and formatted birthday
DateFormat df = DateFormat.getDateInstance();
System.out.println("Hello " + Name + ". You were born on " + df.format(dateBday) + ". ");
// variables for year and days of year
iCalendarNowDayOfYear = calendarNow.get(Calendar.DAY_OF_YEAR);
iCalendarBdayDayOfYear = calendarBday.get(Calendar.DAY_OF_YEAR);
iCalendarNowYear = calendarNow.get(Calendar.YEAR);
iCalendarBdayYear = calendarBday.get(Calendar.YEAR);
int iYearsOld, iDaysOld; int iNewBday, iDaysLeft;
// variables, Formula for years and days old
iYearsOld = iCalendarNowYear - iCalendarBdayYear;
iDaysOld = (iCalendarNowDayOfYear - iCalendarBdayDayOfYear);
iNewBday = (iYearsOld + 1);
iDaysLeft = ( 365 - iDaysOld);
//If your birthday was less than two weeks ago
if (iDaysLeft > 351)
System.out.println( "How did your birthday party go? As of today you are "
+ iYearsOld + " years and " + iDaysOld + " days old." );
//Else if your birthday is in two weeks or less
else if (iDaysLeft <= 14)
System.out.println( "Hello, " + Name + ". I am excited to let you know that you "
+ "will be turning" + iNewBday + "in" + iDaysLeft + "days. "
+ "Do you have any birthday plans?" );
//All others
else
System.out.println( "Hello " +Name + ". You are "+ iYearsOld + "years and "
+ iDaysOld + " days old." );
}
}
Correctly stops at first instance of "if" since iDaysLeft is > 351 (birthday within past 2 weeks)
run:
Hi! Please enter your name.
Bob
Please enter the year (yyyy) you were born.
2000
Please enter the month (mm) you were born.
03
Please enter the date (dd) you were born.
26
Hello Bob. You were born on Mar 26, 2000.
How did your birthday party go? As of today you are 12 years and -2 days old.
BUILD SUCCESSFUL (total time: 6 seconds)
Should have gone to second instance of "if" since iDaysLeft is <= 14 (birthday is in less than 2 weeks)
run:
Hi! Please enter your name.
Bob
Please enter the year (yyyy) you were born.
2000
Please enter the month (mm) you were born.
03
Please enter the date (dd) you were born.
27
Hello Bob. You were born on Mar 27, 2000.
How did your birthday party go? As of today you are 12 years and -3 days old.
BUILD SUCCESSFUL (total time: 12 seconds)
Should have gone to third "if" since birthday falls outside parameters of first two "ifs" (birthday is not in 2 weeks or less and was not in past 2 weeks)
run:
Hi! Please enter your name.
Bob
Please enter the year (yyyy) you were born.
2000
Please enter the month (mm) you were born.
08
Please enter the date (dd) you were born.
01
Hello Bob. You were born on Aug 1, 2000.
How did your birthday party go? As of today you are 12 years and -130 days old.
BUILD SUCCESSFUL (total time: 6 seconds)
Re: Using If/Else - Defaults to first variable and doesn't go further...
Use System.out.println statements to help you debug your program. It is likely that a key variable is not holding the value that you think that it is holding.
Re: Using If/Else - Defaults to first variable and doesn't go further...
I think I need new variables for my If/Else - now I just have to figure out what that is...
iDaysOld = Today's Julian date - BirthDay Date
iDaysLeft = Number of days in year - Today's Julian date
I think it needs to be the difference between today's julian date and the julian date of the birthday so that it will correctly calculate the plus/minus/equal number of days. Am I even close on this?
Re: Using If/Else - Defaults to first variable and doesn't go further...
I think I am making this way harder than it has to be.