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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hashtable ClassCastException

Status
Not open for further replies.

bobrivers2003

Technical User
Oct 28, 2005
96
0
0
GB
Make a brew first!

I am creating some classes that calculate the checksum of all files within a ear archive and saves these results to a report.txt file. It works a treat and computes a checksum for all files even if there are within a directory, within a directry etc.

The problem I am having is printing the results to a txt file. I have two columns: col 1 = path and file name, col 2 = checksum value. Because the paths and filenames vary greatly in length the column of checksum values is a mess and not aligned. I wish to change this.

A friend reccommended putting the values (filename, checksum) into a hashmap/table, order the entries, work out the length of each entry and based on the length add certain number of \t's to the output to sort out alignment.

I have never touched hashmaps/tables and I am having a nightmare. I want the path/filename to be the key and the checksum result to be the entry.

writeReport(string result, File fileName) is method in ReportWriter class that writes to report.txt

The error I get is "ERROR java.lang.ClassCastException: java.util.Hashtable$Entry"

If there is any advice I would greatly appreciate it. Thank you for reading this thread. :)

My Code:

import java.io.File;
import java.util.Hashtable;
import java.util.zip.CRC32;
import java.util.HashMap;
import java.util.Vector;
import java.util.Iterator;

public class ListDirectory {

static MD5Checksum md5 = null;
static ComputeCRC32 crc = null;
static ReportWriter rw = null;
static File checkFile = null;
static Hashtable map;

public static void listDirectory(String tempDir, String reportFile) {

map = new Hashtable();
md5 = new MD5Checksum();
crc = new ComputeCRC32();
rw = new ReportWriter();
Vector zipFile = new Vector();

File dir = new File(tempDir);
File[] fileArray = dir.listFiles();

if (fileArray == null) {
// Either dir does not exist or is not a directory
} else {
for (int i = 0; i < fileArray.length; i++) {

String stringFile = fileArray.toString();

if (fileArray.isDirectory()) {

System.out.println("DIRECTORY " + fileArray);
rw.writeReport(fileArray + " IS A DIRECTORY ", reportFile);

String fileDir = fileArray.toString();
listDirectory(fileDir, reportFile);

} else {

try {

System.out.println("FILE " + fileArray);
//String result = md5.getMD5Checksum(stringFile);
String result = String.valueOf(crc.getChecksumValue(new CRC32(), stringFile));
System.out.println(fileArray + " CRC32 " + result);
map.put(fileArray, result);
formatReport(map, reportFile);

//rw.writeReport(fileArray + "" + '\t' + '\t' + '\t' + '\t'+ '\t' + '\t' + " CRC32 " + result, reportFile);

} catch (Exception e) {

System.out.println("ERROR " + e);
}
}
}
}

}

public static void formatReport(Hashtable map, String reportFile) {


Hashtable htMap = map;
Iterator iter = htMap.entrySet().iterator();
int maxSize = 0;
while (iter.hasNext()) {
String fileName = (String) iter.next();
String crc = (String)map.get(fileName);
System.out.println("ITER" + iter);

if (fileName.length() > maxSize) {
maxSize = fileName.length();
// Do some stuff to add tabs etc
// and write to file

}
}
}
}
 
The complete stacktrace would help. Maybe when you're trying to print the iter.

Cheers,
Dian
 
The rest of the error ... (its called a stack trace because it is a trace of the method calls on the process stack)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thats an interesting point about posting code.
I just could not really be bothered to read through a bunch of posted code, badly formatted.
Though could I be bothered to read it even if it was well formatted ? Possibly not ...

What would be really good is if posters posted code that was well formatted - and was a little bit more cut-down (ie had analized the stack trace - and could post an example which was a little bit more specific).

Still ... most people don't so ...

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
BTW - your error ...

You are adding File objects to the hashtable, and then attempting to cast them out to String objects. This results in a cast exception - because they are different classes.

Either :

1) Add them in as String objects (use the File.toString() method)
2) Cast them out as File objects, rather than String, and use the File.toString() method to get your String.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top