Hello,
I have a program called Date. While this program compiles and runs, I have noticed something weird. I have an output line that displays my values twice.
Sample Run:
The word date output is: February 12 2009
The numeric output of the date is: 2/12/2009
The next incremented date is: 2/12/2009

The next incremented date is: 2/13/2009
Why is that? I have the same issue in my other program. There is something i am overlooking on the consistent basis
Any help will be appreciated:
Code:

// Main

import java.io.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws IOException
    {
       BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); // Calling on java class which allow to read user input
    // Local Variables
    String temp;
    int month;
    int day;
    int year;

    Date myDate = new Date();
    // Reading user input for month
    System.out.println("Please enter month in numeric format 1-12:");
    temp = stdin.readLine();
    month = Integer.parseInt(temp);                                     // Converting a string into int
    // Reading user input for day of the month
    System.out.println("Please enter day of the month in numeric format 1-31:");
    temp = stdin.readLine();
    day = Integer.parseInt(temp);                                     // Converting a string into int
    // Reading user input for the year
    System.out.println("Please enter year:");
    temp = stdin.readLine();
    year = Integer.parseInt(temp);                                  // Converting a string into int
    // Calling methods from a class Date
    myDate.outputDate(month,day,year);
    myDate.increment(month,day,year);
    }

}

// Class Date

public class Date
{
    // instance variables - month date and a year
    final int MAX_DAYS = 31;        // Constant that cannot be changed is the number of days in one month
    private int month;
    private int day;
    private int year;


    /**
     * Constructor for objects of class Date
     */

    // 3 argument Constructor
    public Date()
    {
    // 3 argument Constructor
    int month = 0;
    int day =   0;
    int year =  0;

    }

    /**
     * increment method
     * Incrementing the date to the next day
     * @param month, day, year
     * @return   i (incremented date)
     */

    public int increment(int arg1, int arg2, int arg3)
    {
        int i;
        month = arg1;
        day = arg2;
        year = arg3;

        for (i = day; i <= day+1; i++)
        {
        System.out.println( "The next incremented date is: " + month + "/" + i + "/" + year);
        }
        return i;
    }
    /**
     * Output date method
     * This method gives an output in word and numeric format simultaneously
     *
     * @param arg1, arg2, arg3
     * @return   date
     */
      public int outputDate(int arg1, int arg2, int arg3)
    {
      // Local variables
      int date =0;
      month = arg1;
      day = arg2;
      year = arg3;
      // This loop provides user with word and numeric output of a date
      if(month <= 12)
      {
        if (month == 1)
      {
          System.out.println("The word date output is: January " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 2)
      {
          System.out.println("The word date output is: February " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 3)
      {
          System.out.println("The word date output is: March " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 4)
      {
          System.out.println("The word date output is: April " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 5)
      {
          System.out.println("The word date output is: May " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 6)
      {
          System.out.println("The word date output is: June " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 7)
      {
          System.out.println("The word date output is: July " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 8)
      {
          System.out.println("The word date output is: August " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 9)
      {
          System.out.println("The word date output is: September " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 10)
      {
          System.out.println("The word date output is: October " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 11)
      {
          System.out.println("The word date output is: November " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }
       else if (month == 12)
      {
          System.out.println("The word date output is: December " + day + " " + year);
          System.out.println("The numeric output of the date is: " + month + "/" + day + "/" + year);
        }

    }
    else {
        System.out.println("There are only 12 months in one year, Please eneter number between 1 and 12: ");
    }
      return date;

    }

}

Recommended Answers

All 2 Replies

You use a for loop in your increment method, so it prints out both lines. Removing the for loop and using a simple println will take out the duplicated line. Just be sure to increment the day.

public void increment(int arg1, int arg2, int arg3)
    {
        int i;
        month = arg1;
        day = arg2;
        year = arg3;

        //for (i = day; i < day+1; i++)
       // {
        System.out.println( "The next incremented date is: " + month + "/" + (day+1) + "/" + year);
        //}
        //return i;
    }

Thank you. I was able to fix this bug.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.