Results 1 to 4 of 4
  1. #1
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Using If/Else - Defaults to first variable and doesn't go further...

    Java 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)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default 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?

  4. #4
    felonee is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default 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.

Similar Threads

  1. Replies: 3
    Last Post: 02-14-2012, 10:45 PM
  2. Replies: 11
    Last Post: 07-07-2010, 06:02 AM
  3. GUI help - Text Box Defaults to Array Value
    By ejs7597 in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 05:48 AM
  4. Content assist defaults
    By tenmiles in forum Eclipse
    Replies: 0
    Last Post: 03-24-2009, 06:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •