Results 1 to 6 of 6
Thread: Incrementing variables
- 11-06-2012, 04:57 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
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()); } }
- 11-06-2012, 05:09 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 11-06-2012, 05:14 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
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
- 11-06-2012, 05:20 PM #4
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
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()); } }
- 11-06-2012, 05:34 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 11-06-2012, 05:44 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
For Loop NOT Incrementing...
By HSKrustofsky in forum New To JavaReplies: 2Last Post: 09-04-2011, 11:13 PM -
Incrementing by more than 1
By davidq23 in forum New To JavaReplies: 2Last Post: 07-27-2011, 01:12 AM -
Help with incrementing numbers in a GUI
By jdg951 in forum New To JavaReplies: 6Last Post: 01-18-2011, 03:23 AM -
incrementing elements in an array
By JavaNubb in forum New To JavaReplies: 7Last Post: 08-20-2010, 03:15 AM -
Incrementing and loops
By Rubidoux in forum New To JavaReplies: 1Last Post: 04-24-2010, 02:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks