• Post Reply Bookmark Topic Watch Topic
  • New Topic

While Loop  RSS feed

 
Carlos Cueto
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I need some help with this method, it seems to run as an infinite loop every time it's called in the main method. I don't understand why it's running infinite loop if I change the loop control variable in the while loop body. Thanks in advance.

 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing value of variable year1 does not change state of nextYearNewYear object, hence infinite loop.
 
Carlos Cueto
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But year1 is the year parameter of the second Date object, if I add 1 to it in the loop, shouldn't the year change by 1 and thus the day of the week change as well?
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Object nextYearNewYear doesn't see any changes made on year1. Object nextYearNewYear used the value the variable held at the time the object was created. It knows nothing about the variable or any changes made to it.
Try adding nextYearNewYear = new Date (1,1,year1); after year++;
 
Nam Ha Minh
Ranch Hand
Posts: 515
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carlos Cueto wrote:But year1 is the year parameter of the second Date object, if I add 1 to it in the loop, shouldn't the year change by 1 and thus the day of the week change as well?

You are comparing two different Date objects which are never equal, thus the loop runs infinitely.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!