| | |
Hash Table---Urgent Help needed!
![]() |
•
•
Join Date: Mar 2008
Posts: 31
Reputation:
Solved Threads: 0
I have this code here:
it uses text files and hash tables to keep some student records:
can somebody help me with the 'FindEntry' Method, i don't really get why it doesn't work....
thanks
it uses text files and hash tables to keep some student records:
can somebody help me with the 'FindEntry' Method, i don't really get why it doesn't work....
public class HashTable {
public static class StudentRecord implements Serializable {
public String name,family;
public int stNr,flag;
public static final int SIZE=68;//record size
public StudentRecord() {
name=" ";family=" ";stNr=0;flag=0;
}
public StudentRecord(String name,String family,int stNr,int flag){
this.name=name;
this.family=family;
this.stNr=stNr;
this.flag=flag;
}
public StudentRecord(int key,String value){
stNr=key;
setValue(value);
}
public String getValue(){
return (family+"#"+name+"#");
}
public void setValue(String value){
String t[]=new String[2];
int i=0;
StringTokenizer tokens=new StringTokenizer(value,"#");
while(tokens.hasMoreTokens()){
t[i]=tokens.nextToken();
i++;
}
this.family=t[0];
this.name=t[1];
}
public void write(RandomAccessFile file)throws IOException{
writeName(file,name);
writeName(file,family);
file.writeInt(stNr);
file.writeInt(flag);
}
public void writeName(RandomAccessFile file,String str)throws IOException{
StringBuffer buffer=null;
if(str!=null)
buffer=new StringBuffer(str);
else
buffer=new StringBuffer(15);
buffer.setLength(15);
file.writeChars(buffer.toString());
}
public void read(RandomAccessFile file)throws IOException{
name=readName(file);
family=readName(file);
stNr=file.readInt();
flag=file.readInt();
}
public String readName(RandomAccessFile file)throws IOException{
char name[]=new char[15];
for(int i=0;i<name.length;i++)
name[i]=file.readChar();
return new String(name).replace('\0',' ');
}
}
protected static class DefaultEqualityTester implements EqualityTester {
....
}
protected int n = 0;
protected int N;
File filename1=new File("D:\\b.txt");
RandomAccessFile file=new RandomAccessFile(filename1,"rw");//readwrite
File filename2=new File("D:\\c.txt");
RandomAccessFile file2=new RandomAccessFile(filename2,"rw");
protected EqualityTester T; // the equality tester
protected int scale, shift; // the shift and scaling factors
public HashTable() throws FileNotFoundException, IOException {
N =1023; // default capacity
T = new DefaultEqualityTester(); // use the default equality tester
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
StudentRecord record=new StudentRecord();
for(int i=0;i<N;i++)
record.write(file);
}
public HashTable(int bN, EqualityTester tester) throws FileNotFoundException, IOException {
N = bN;
T = tester;
java.util.Random rand = new java.util.Random();
scale = rand.nextInt(N-1) + 1;
shift = rand.nextInt(N);
StudentRecord record=new StudentRecord();
for(int i=0;i<N;i++)
record.write(file);
}
protected int findEntry(Object key) throws InvalidKeyException, IOException {
int avail = 0;
checkKey(key);
int i = hashValue(key);
int j = i;
StudentRecord currentRecord=new StudentRecord();
do {
file.seek(i*StudentRecord.SIZE);
if (currentRecord == null) return -i - 1; // entry is not found
if (currentRecord.flag ==0) { // bucket is deactivated
avail = i; // remember that this slot(سوراخ) is available
i = (i + 1) % N;
}
else if (T.isEqualTo(key,currentRecord.stNr)) // we have found our entry
return i;
else // this slot is occupied--we must keep looking
i = (i + 1) % N;
} while (i != j);
return -avail - 1; // entry is not found
}
public Object get (Object key) throws InvalidKeyException, IOException {
StudentRecord currentRecord=new StudentRecord();
int i = findEntry(key); // helper method for finding a key
if (i < 0) return null; // there is no value for this key
file.seek(i*StudentRecord.SIZE);
return currentRecord.getValue(); // return the found value in this case
}
public void put (int key, String value) throws InvalidKeyException, IOException {
StudentRecord currentRecord = new StudentRecord(key, value);
if (n >= N/2) rehash(); // rehash to keep the load factor <= 0.5
int i = findEntry(key); //find the appropriate spot for this entry
if (i < 0) { // this key does not already have a value
file.seek((-i-1)*StudentRecord.SIZE); // convert to the proper index
currentRecord.write(file);
n++;
// there was no previous value
}
else { // this key has a previous value
file.seek(i*StudentRecord.SIZE);
currentRecord.write(file);
}
}
public void remove (Object key) throws InvalidKeyException, IOException {
// StudentRecord currentRecord=new StudentRecord();
int i = findEntry(key); // find this key first
if (i < 0) // nothing to remove System.out.println("this item not found!");
else{
StudentRecord currentRecord=new StudentRecord();
while(true){
do{
currentRecord.read(file);
}while(currentRecord.stNr!=key);
currentRecord.flag=0;
}
// file.seek(i*StudentRecord.SIZE);
// currentRecord.flag=0;
}
n--;
}
..
.
.
.thanks
Last edited by scream2ice; Jun 20th, 2009 at 9:03 am. Reason: there wasn't a need for all code to be displayd
I guess you need to assign currentRecord to output of file.seek .
![]() |
Similar Threads
- Reading txt file into Hash Table (C++)
- FRONTECH tv tuner card - stuck lost - urgent help needed (PCI and Add-In Cards)
- Hash Table template implementation help (C++)
- C++hash table lookin' 4 help~~ (C++)
- URGENT HELP NEEDED - critically important...!!! (Windows NT / 2000 / XP)
- extreme urgent help needed (Geeks' Lounge)
- Printing a Hash Table (C++)
- Pass XML file contents to a hash table. (Java)
- compile error-chained hash table c++ (C++)
Other Threads in the Java Forum
- Previous Thread: chat module
- Next Thread: display using Jslider
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






