943,755 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 922
  • Java RSS
Jun 20th, 2009
0

Hash Table---Urgent Help needed!

Expand Post »
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 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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
scream2ice is offline Offline
33 posts
since Mar 2008
Jun 22nd, 2009
0

Re: Hash Table---Urgent Help needed!

I guess you need to assign currentRecord to output of file.seek .
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: chat module
Next Thread in Java Forum Timeline: display using Jslider





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC