Logout

RAF Adding & Searching By Hashing

GUIForTheRAF.java
/Users/adelaide/Public/Netbeans - All JSR Projects/HashableRAF2011/src/newpackage/GUIForTheRAF.java
 23 public class GUIForTheRAF extends javax.swing.JFrame {
 24 
 26     public GUIForTheRAF() {
 27         initComponents();
 28         myInitComonents();
 29     }
 30     private RandomAccessFile raf;
 31     private int counterForSequential = 0;
 32     private int recordLength = 2000;//Was this the problem???
 33     private int MAXNUMRECORDS = 10;
 34 
 35     private void myInitComonents() {
 36         try {
 37             raf = new RandomAccessFile("another raf - 7.txt", "rw");//Should make a new file each test
 38             //And definitely have a Save dialogue box for real thing.
 39             for (int i = 0; i < MAXNUMRECORDS * recordLength; i++) {//IMPORTANT to have * recordLength, full file to work with, and
 40                 //no EndOfFile error.
 41                 try {
 42                     raf.writeUTF("");//This may be the secret; rather than putting in 999 or *** etc.
 43                     raf.writeUTF("");
 44                 } catch (IOException ex) {
 45                 }
 46 
 47             }
 48         } catch (FileNotFoundException ex) {
 49         }
 52     }
GUIForTheRAF.java
/Users/adelaide/Public/Netbeans - All JSR Projects/HashableRAF2011/src/newpackage/GUIForTheRAF.java
314     private void hashAddButtonMouseReleased(java.awt.event.MouseEvent evt) {                                            
315         try {
316             String nameToAdd = hashInputNameTextField.getText();
317             int hashTotal = 0;
318             for (int i = 0; i < nameToAdd.length(); i++) {
319                 hashTotal += nameToAdd.charAt(i);
320             }
321             int hashValue = hashTotal % MAXNUMRECORDS;
322             int initialPosition = hashValue * recordLength; //This can be the line where you forget to x by recordLength
323             System.out.println("initialPosition: " + initialPosition);
324 
325             //String readInFromRAF = raf.readUTF(); //Here's where my issue was, because having readUTF, it advances <-----------
327             int pointer = initialPosition;
328             boolean freeSpaceFound = false;
329             do {
330                 raf.seek(pointer);
331                 String readIn = raf.readUTF();
332                 if (readIn.equals("")) {
333                     freeSpaceFound = true;
334                 } else {
335                     pointer += recordLength;
336                 }
337             } while (!freeSpaceFound);
338             raf.seek(pointer);//Take it back to where it's Ok to write.
339             raf.writeUTF(hashInputNameTextField.getText()); //***DO NOT PUT \n !!!!!
341             raf.writeUTF(hashInputNumberTextField.getText());
342 
343             hashInputNameTextField.setText("");
344             hashInputNumberTextField.setText("");
345         } catch (IOException ex) {
346             Logger.getLogger(GUIForTheRAF.class.getName()).log(Level.SEVERE, null, ex);
347         }
348     }                                           
349 
350     private void hashSearchButtonMouseReleased(java.awt.event.MouseEvent evt) {                                               
351 
352         try {
353             String nameToSearch = hashInputSearchNameTextField.getText();
354             int hashTotal = 0;
355             for (int i = 0; i < nameToSearch.length(); i++) {
356                 hashTotal += nameToSearch.charAt(i);
357             }
358             int hashValue = hashTotal % MAXNUMRECORDS;
359             int initialPosition = hashValue * recordLength;
360             int pointer = initialPosition;
361 
362             boolean foundKeyLookingFor = false;
363             do {
364                 raf.seek(pointer);
365                 String readIn = raf.readUTF();
366                 if (readIn.equals(hashInputSearchNameTextField.getText())) {
367                     foundKeyLookingFor = true;
368                     System.out.println("found");
369                 } else {
370                     pointer += recordLength;
371                     raf.seek(pointer);//IMPORTANT HERE IS TO seek AGAIN, even though a readUTF should work, shouldn't it?
372                 }
373             } while (!foundKeyLookingFor);
374             raf.seek(pointer);//Take it back to where it's Ok to read.
375             raf.readUTF();
376             //Doesn't need to return to a variable to "advance", but need to advance again to the second field.
377             hashDisplayNumberTextField.setText(raf.readUTF());
378             //So here at the end, two readUTFs in a row advance things properly, but above
379             //in the loop, another seek was needed each time.... "When in doubt, seek!"
380 
381         } catch (IOException ex) {
382         }
384     }