• 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

Date and loop question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this snippet of code that I'm having a problem with and hope someone here might be able to help. In brief what it should do is search a database for any entries with the current date. If it is not found, it should then generate entries in the database for that date and any other dates for that week starting with Tuesday. Once through the loop, the new entries should be added to the list and prepared for the view. What I get is the loop running a second time followed by a unique constraint violation error from the database. Now I'm not sure if it's running through every entry and performing the check or not but it seems that's what is happening to me and so far I haven't been able to stop it. If anyone is able to point out my mistakes, I'd appreciate it.

 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesse,
Welcome to CodeRanch!

The findAllForecasts() method appears to call findAllForecasts() in the if statement. This looks like infinite recursion (where the program would never end if it didn't throw an exception). Here's the flow I'm seeing:

1) Call findAllForecasts()
2) Create new date and DateFormatter
3) Log
4) Call DAO to find forecasts
5) Enter loop
6) Log
7) if need to create, create and go back to step 1

Am I missing something?
 
Jesse McCullough
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne,

Thanks for the welcome and no, you haven't missed anything. That's exactly what I'm aiming for.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse McCullough wrote:Thanks for the welcome and no, you haven't missed anything. That's exactly what I'm aiming for.


OK, so how does your code differ from what Jeanne wrote? She also gave you a big clue.

Winston
 
Jesse McCullough
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see two problems. One is the recursion caused by findAllForecasts() in the loop. Easily fixed. The other is that I keep going through the loop for every element in the list, when what I need to do is check just the last one, maybe. But that poses another problem because I'm creating dates for the whole week. And that's what I've been trying to get my head around. What's the best way to check for dates and create them in this situation?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse McCullough wrote:I can see two problems. One is the recursion caused by findAllForecasts() in the loop. Easily fixed. The other is that I keep going through the loop for every element in the list, when what I need to do is check just the last one, maybe. But that poses another problem because I'm creating dates for the whole week. And that's what I've been trying to get my head around. What's the best way to check for dates and create them in this situation?


Well, the first thing is to deal with one thing at a time.

So: fix your recursion problem. Now what happens? Maybe what you expect (ie, you "keep going through the loop for every element in the list"), but make sure.

You might also be better off writing the code so that it only gets ONE date (ie, the first one you're interested in) to begin with. Once you know THAT works, add the logic to get a whole week (and for that, you'll find the Calendar.add() method very useful).

Do you get my point? Test small things, and make sure they work, before you start adding new ones.

HIH

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic