AP Comp Sci Help

<p>Alright. My Comp Sci teacher gave us a hard-@$$ project to count the number of days between an inputed date and today’s date. We can’t use any java classes or any functions online, but we have to do it by ourselves.</p>

<p>Can you guys critique my code and tell me if you see any holes?</p>

<p>Filename: Countday.java



// Import the stuff for the input function
import java.io.*;</p>

<p>// Import the stuff for the date fuction
import java.util.*;</p>

<p>public class Countday
    {
    // Create a single shared BufferedReader for keyboard input
    private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) );</p>

<pre><code>public static void main(String args[]) throws IOException
    {
    // Define Variables
    int cycle = 23; // Rhythm Cycle

    // Prompt the user for birth month
    System.out.print( "Enter your the month in which you were born:" );

    // Read a birth month from the user.
    String smonth = stdin.readLine();

    // Converts inputed string into an int
    int month = Integer.parseInt(smonth);

    // Prompt the user for birth day
    System.out.print( "Enter your the day in which you were born:" );

    // Read a birth month from the user.
    String sday = stdin.readLine();

    // Converts inputed string into an int
    int day = Integer.parseInt(sday);

    // Prompt the user for birth month
    System.out.print( "Enter your the year in which you were born:" );

    // Read a birth month from the user.
    String syear = stdin.readLine();

    // Converts inputed string into an int
    int year = Integer.parseInt(syear);

    // Initialize variable to count days
    int x = 0;

    // Define Today's Date Variable
    Date d = new Date(); // Define date variable
    int today_month = d.getMonth()+1;  // Define today's month
    int today_day = d.getDate(); // Define today's day
    int today_year = d.getYear()+1900;  // Define today's year


    // Count to the next month
    // but first let's see if we're in the same month
    if(month == today_month && year == today_year)
        {
        x = today_day-day;

        today_day = day;
        }else{
        // You have to take the number of days in the month, add 1, and then subtract the day
        // It is b/c you are setting day = 1 and it must include that day
        if(month == 4 || month == 6 || month == 9 || month == 11)
            {

            x = 31-day;
            day = 1;
            month++;
            }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
            {
            x = 32-day;
            day = 1;
            month++;
            }else if(month == 2 && year%4 == 0)
            {
            x = 30-day;
            day = 1;
            month++;
            }else if(month == 2 && year%4 != 0)
            {
            x = 29-day;
            day = 1;
            month++;
            }

        // Fix the problem if the month goes up to 13, make the month 1 and increase the year
        if(month == 13)
            {
            month = 1;
            year++;
            }
        }

    System.out.println("Initial Loop: "+month+"/"+day+"/"+year+" - Days: "+x);


    /* This will get us to the current year
    // If you notice - if you are in a different year, but within 365 days of today's date, it
    // won't use this while loop, but rather wait and use the month while loop
    */
    while((year != today_year) && (today_year-year > 1))
        {
        // Add 366 if we're in a leap year
        if(year%4==0)
            {
            x=x+366;
            year++;

            // Add 365 if we're not in a leap year
            }else if(year%4 != 0){
            x=x+365;
            year++;
            }
        System.out.println("Year While Loop: "+month+"/"+day+"/"+year+" - Days: "+x);
        }

    // This will get us to the current month
    while((month != today_month) || (year != today_year))
        {
        // You have to take the number of days in the month, add 1, and then subtract the day
        // It is b/c you are setting day = 1 and it must include that day
        if(month == 4 || month == 6 || month == 9 || month == 11)
            {
            x = x+(31-day);
            day = 1;
            month++;
            }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
            x = x+(32-day);
            day = 1;
            month++;
            }else if(month == 2 && year%4 == 0){
            x = x+(30-day);
            day = 1;
            month++;
            }else if(month == 2 && year%4 != 0){
            x = x+(29-day);
            day = 1;
            month++;
            }

        // Fix the problem if the month goes up to 13, make the month 1 and increase the year
        if(month == 13)
            {
            month = 1;
            year++;
            }

        System.out.println("Month While Loop: "+month+"/"+day+"/"+year+" - Days: "+x);

        }

    // This will get us to the current day
    while(day != today_day)
        {
        x = x+(today_day-day);

        day = today_day;

        System.out.println("Day While Loop: "+month+"/"+day+"/"+year+" - Days: "+x);
        }



    // Show the number of days from birth till today
    System.out.println("You were born on "+smonth+"/"+sday+"/"+syear+". Thus, you are "+x+" days old.");

    }
}
</code></pre>

<p>

</p>

<p>Thanks.</p>