Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

logic error 1

Status
Not open for further replies.

javanemo

Technical User
Sep 9, 2003
14
AU
hi I am trying to write a simple logic

say example
<pre>
a=20030930;
b=20031005;
c=20031130;
d=20031231;
e=20030829;

while (1){
..
if(currentdate <= dateline)
{
print(date);
}
}

where dateline is a list of all the dates eg a, b, c, d, e
But my above codes prints
b
c
d
How can I make it such that the date only appears once? Like the next nearest date is b? Then when reaches b, it will just display c?
</pre>
 
You date must be sorted, so
(I consider date as String in my case, you should do it in Date format)
String d[] = new String[100];
d[0]=&quot;20030829&quot;;
d[1]=&quot;20030930&quot;;
d[2]=&quot;20031005&quot;;
d[3]=&quot;20031130&quot;;
d[4]=&quot;20031231&quot;;
...


The following is the code


class scompare
{
public static void main(String args[])
{

String d[] = new String[5];
String currentDate = &quot;20031006&quot;;
d[0]=&quot;20030829&quot;;
d[1]=&quot;20030930&quot;;
d[2]=&quot;20031005&quot;;
d[3]=&quot;20031130&quot;;
d[4]=&quot;20031231&quot;;
int matchNum = 0, suitableDay = 0;

for (int i=4;i>=0;i--)
{
if (currentDate.compareTo(d)==0)
{
suitableDay = i;
matchNum++;
break;
}
if (currentDate.compareTo(d)<0)
{
suitableDay = i;
matchNum++;
System.out.println(suitableDay);
continue;
}
else
break;
}
if (matchNum==0) System.out.println(&quot;No match&quot;);
else
System.out.println(&quot;suitable date &quot;+d[suitableDay]);
}
}
//************ Please Mark this post as helpful if the code is suitable for you
 
once you've found the &quot;date&quot; you want to display, simply &quot;break;&quot; this will drop you out of the loop. if that's what you wanted to do?
 
thanks for the post
what happens if I do now know how many dates will there be in the array?
i am getting the dates from the sql table and dates are not sorted
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top