943,733 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 468
  • Java RSS
May 2nd, 2009
0

Main displaying values twice

Expand Post »
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;

    }

}
Similar Threads
Reputation Points: 36
Solved Threads: 0
Junior Poster in Training
grisha83 is offline Offline
70 posts
since Sep 2008
May 2nd, 2009
0

Re: Main displaying values twice

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.

Java Syntax (Toggle Plain Text)
  1. public void increment(int arg1, int arg2, int arg3)
  2. {
  3. int i;
  4. month = arg1;
  5. day = arg2;
  6. year = arg3;
  7.  
  8. //for (i = day; i < day+1; i++)
  9. // {
  10. System.out.println( "The next incremented date is: " + month + "/" + (day+1) + "/" + year);
  11. //}
  12. //return i;
  13. }
Last edited by LevelSix; May 2nd, 2009 at 8:40 pm.
Reputation Points: 22
Solved Threads: 3
Junior Poster in Training
LevelSix is offline Offline
59 posts
since May 2008
May 2nd, 2009
0

Re: Main displaying values twice

Thank you. I was able to fix this bug.
Reputation Points: 36
Solved Threads: 0
Junior Poster in Training
grisha83 is offline Offline
70 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Binary Tree and recursion
Next Thread in Java Forum Timeline: Collaboration between java and vb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC