• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to display one image per day from an array?

 
Jessica Patrick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai..

I need to display one image per day from an array depending upon the day in jsp/servlet.

Intially i am having 5 images in an array.

Then i want to display

sunday --- 1st image
monday--- 2nd image
tuesday -- 3rd image
wednesday--4th image
thursday -- 5th image
friday -- 1st image
Saturday-- 2nd image

number of images is dynamically changing.

if the array has one image.. daily that one image should display.
if the array has two image, then 1st day--- 1st image. 2nd day--- 2nd image, 3rd day--- again 1st image.

Can any one please tell me the logic to do this? waiting for your reply.

Thank you...
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You must be using a timer task

Regards,
Srikkanth.M
 
Jessica Patrick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timer task means?
Can you please explain in detail?
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where exactly are you stuck? Is it the displaying image part or the scheduling part?
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can make use of the Timer class and TimerTask available in J2SE to schedule an event.
You create a task that iterates over the collection of the images and picks one image object.

Refer Timer API


Regards,
Srikkanth.M
 
Jessica Patrick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code. array length is number of records available in the database. that is retrived in a query.

Calendar cal = Calendar.getInstance();
int dayofweek = cal.get(cal.DAY_OF_WEEK);

if(arraylength==2)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=1;
else if(dayofweek==4)
imagenumber=2;
else if(dayofweek==5)
imagenumber=1;
else if(dayofweek==6)
imagenumber=2;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==3)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=1;
else if(dayofweek==5)
imagenumber=2;
else if(dayofweek==6)
imagenumber=3;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==4)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=1;
else if(dayofweek==6)
imagenumber=2;
else if(dayofweek==7)
imagenumber=3;
}
else if(arraylength==5)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=5;
else if(dayofweek==6)
imagenumber=1;
else if(dayofweek==7)
imagenumber=2;
}
else if(arraylength==6)
{
if(dayofweek==1)
imagenumber=1;
else if(dayofweek==2)
imagenumber=2;
else if(dayofweek==3)
imagenumber=3;
else if(dayofweek==4)
imagenumber=4;
else if(dayofweek==5)
imagenumber=5;
else if(dayofweek==6)
imagenumber=6;
else if(dayofweek==7)
imagenumber=1;
}
else if(arraylength==7)
imagenumber = dayofweek;
else
imagenumber=arraylength;


I am manually checking array length and assigning the image. I need some dynamic calculation for assigning the image. Also this code works only upto 7 images. I need to display even the array length is 25, 50 etc...

Kindly check it out and give me a solution.
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Try reading up on the modulo (%) operator. I guess it should be quite helpful.
[ May 17, 2007: Message edited by: Anupam Sinha ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Jessica",
Welcome to JavaRanch!

We're pleased to have you here with us in the JSP forum, but there
are a few rules that need to be followed, and one is that proper names are
required. Please take a look at the
JavaRanch Naming Policy and
adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

You can change it here
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64958
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with a Timer. It's just a simple algorightms question regarding dealing with modulus. It really has nothing to do with JSP either so it's been moved to a general Java forum.
 
Srikkanth Mohanasundaram
Ranch Hand
Posts: 243
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah... I forgot Date and Time API's
 
Jessica Patrick
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai..

I used the Calendar instance to get the day of the week.

But my question is how can i get the image sequence dynamically.
i need the calculation part alone.

Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic