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....

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 [B]findEntry[/B](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

I guess you need to assign currentRecord to output of file.seek .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.