aspose file tools*
The moose likes Java in General and the fly likes handling with hashtable and sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "handling with hashtable and sorting" Watch "handling with hashtable and sorting" New topic
Author

handling with hashtable and sorting

vijaykumar shah
Greenhorn

Joined: Jul 02, 2003
Posts: 23
Hello,
i am new to this forum.. so let's introduce myself i am a java programmer from india, my name is vijay
According to the requirement of displaying the records in bunch of 5 ... what i did is fetched the record from database and saved all the data and value into the hash table... and then i wrote a bean for sorting the code of which can be followed here ... please see below

rs3 = stmt3.executeQuery(query3);
Hashtable avlperday = new Hashtable ();
while (rs3.next())
{
++rowcount;
avlperday.put(rs3.getDate("hotelroomtyperatefromdate") , rs3.getString("hotelroomtypenoofavailability"));
}

if (avlperday.size() > 0)
{
sarray my_arry1 = new sarray();


for (Enumeration e = avlperday.keys(); e.hasMoreElements()
{
Date dte = (Date)e.nextElement();
String rateondate = avlperday.get(dte).toString();
sortable s22 = new sortable ( dte , rateondate );
my_arry1.add ( s22 );
}

Collections.sort ( my_arry1 );
int lengthperrow = 5;
int columncount = rowcount/lengthperrow;
int extracolumncount = rowcount%lengthperrow;
int counter = 0;


//vijay shah added
%> <table><tr> <%
for (int ii = 0; ii < columncount ; ii++)
{ %>
<td valign=TOP width="155"><table width="170">
<tr>
<td width="130"> Date </td>
<td width="38"> Avl </td>
</tr><%

for (int jj = 1; jj <= lengthperrow ; jj++)
{
try
{
counter = (ii * lengthperrow) + jj;
System.out.println(counter + "check this");
sortable s22 = (sortable)my_arry1.elementAt (counter);
Date dtev = (Date) s22.getKey();
String dye = Dtfrmt.getDayFromDate(df.format(dtev));
String rtval = (String) s22.getObject();%>
<tr>
<td width="118"> <%=dbf.format(dtev)%> - <%=dye%></td>
<td width="38"> <%=rtval%> </td>
</tr> <%
}
catch(Exception ex)
{
ex.printStackTrace();
}
} // end of for loop
%>
</table></td> <%
} // end of for loop

if (extracolumncount > 0)
{ %>
<td valign=TOP width="155">
<table width="170">
<tr>
<td width="130"> Date </td>
<td width="38"> Avl </td>
</tr>
<%
for (counter = columncount*lengthperrow; counter < rowcount; counter++)
{
sortable s23 = (sortable)my_arry1.elementAt (counter+1);
Date dtev2 = (Date) s23.getKey();
String dye2 = Dtfrmt.getDayFromDate(df.format(dtev2));
String rtval2 = (String) s23.getObject();%>
<tr>
<td width="118"> <%= dbf.format(dtev2)%> - <%=dye2%> </td>
<td width="38"> <%= rtval2%> </td> </tr> <%
} %>
</table></td> <%
}%>
</tr></table> <%
my_arry1.removeElement(my_arry1);
avlperday.clear();
//vijay shah added
} // end of rowcount > 0
else
{ %>
No records found <%
}
Now what i am getting error is Indexoutofboundexception ... it shows me data for big ranges i.e between 01/01/2003 - 0101/2004.. but from where it stops displaying i do not know..
and sometimes it gives me this error of arrayindexoutofexception...
the two files added for sorting are as below..
sarray.java
----------
package hotelier;
import java.util.*;
/**
sarray is a convenience class designed to make ArrayList methods consistent
with Vector. Why did Sun not do this??
*/
/************************************************/
public class sarray extends ArrayList
{
private int last_index = 0;
/************************************************/
public int length ()
{
return ( size ( ) );
}
/************************************************/
public Object elementAt ( int index )
{
last_index = index;
return ( get ( index ) );
}
/************************************************/
public void addElement ( Object o )
{
add ( o );
}
/************************************************/
/**
Sets the last index requested to the supplied value.
*/
public void setLastIndex(int value)
{
last_index = value;
}
/************************************************/
/**
Returns the last index requested by the elementAt method.
*/
public int getLastIndex()
{
return ( last_index );
}

/************************************************/
/**
Make compatible with Vector.
*/
public void removeElementAt ( int index )
{
remove ( index );
}
/************************************************/
/**
Make compatible with Vector.
*/
public void removeElement ( Object obj )
{
int index = indexOf ( obj );
if ( index > -1 ) remove ( index );
}
}
sortable.java
-------------
package hotelier;
import java.io.*;
import java.util.*;
public class sortable implements Comparable
{
private Comparable key = "";
private Object o = null;
/************************************************/
public sortable ( Comparable key , Object o )
{
this.key = key;
this.o = o;
}
/************************************************/
public void setKey ( Comparable key )
{
this.key = key;
}
/************************************************/
public Comparable getKey()
{
return ( key );
}
/************************************************/
public void setObject ( Object o )
{
this.o = o;
}
/************************************************/
public Object getObject()
{
return ( o );
}
/************************************************/
public int compareTo ( Object o )
{
return ( key.compareTo ( ((sortable) o).getKey() ) );
}
}
---------
please give me the solution to the error or is there something i am missing.. to add.. bcz of which i am getting this error or do i need to add something to remove this error.
yr answer are invited...
Thanks
Vijay
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: handling with hashtable and sorting