Results 1 to 6 of 6
  1. #1
    Minionfx is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Incrementing variables

    I am having trouble trying to increment the startYear variable by 1 for 10 consecutive years (e.g. 2012, 2013, 2014, 2015....). This is my first programming course, as well as my first attempts at using iteration. I have attempted a few things such as startYear++ in the loop and right after to see the results, but it did not affect anything. Any information to get me in the right direction would be appreciated.

    Java Code:
    public void printHalloweens(int startYear)
        {
            Date d1 = new Date(10, 31, startYear);
    
            for (int i = 0; i < 10; i++)
            {
                System.out.println(d1.getDayOfWeek());
            }
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Incrementing variables

    You need a new Date for each year.
    Unless there is a setYear() method on that Date class.
    Not sure what Date class it is, though.
    Please do not ask for code as refusal often offends.

  3. #3
    Minionfx is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Incrementing variables

    These were the only mutators I was given for the assignment.

    Java Code:
    Date d = new Date(1,1,2012) ;  // d is January 1, 2012 (a leap year)
    
    d.next() ;                     // d is January 2, 2012
    d.previous() ;                 // d is January 1, 2012
    d.add(31) ;                 	 // d is February 1, 2012
    d.subtract(32) ;               // d is December 31, 2011
    d.next() ;         			 // d is January 1, 2012
    
    d = new Date(3,1,2012) ;  	 // d is March 1, 2012
    d.previous() ;                 // d is now February 29, 2012

  4. #4
    Minionfx is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Incrementing variables

    My objective is to get the day of the week Halloween falls on for the next 10 years. I did this to the loop, but not sure if its really an accurate portrayal.

    Java Code:
    public void printHalloweens(int startYear)
        {
            Date d1 = new Date(10, 31, startYear);
    
    
            for (int i = 0; i < 10; i++)
            {
                d1.add(365);
                System.out.println(d1.getDayOfWeek());
            }
    
        }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Incrementing variables

    Then create a new Date inside the loop for each of the 10 years and print that.
    Please do not ask for code as refusal often offends.

  6. #6
    Minionfx is offline Member
    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Default Re: Incrementing variables

    Alright, thank you for the help.
    :)

Similar Threads

  1. For Loop NOT Incrementing...
    By HSKrustofsky in forum New To Java
    Replies: 2
    Last Post: 09-04-2011, 11:13 PM
  2. Incrementing by more than 1
    By davidq23 in forum New To Java
    Replies: 2
    Last Post: 07-27-2011, 01:12 AM
  3. Help with incrementing numbers in a GUI
    By jdg951 in forum New To Java
    Replies: 6
    Last Post: 01-18-2011, 03:23 AM
  4. incrementing elements in an array
    By JavaNubb in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 03:15 AM
  5. Incrementing and loops
    By Rubidoux in forum New To Java
    Replies: 1
    Last Post: 04-24-2010, 02:04 PM

Tags for this Thread

Posting Permissions

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