• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

While Loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++;
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic