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;
33 private int MAXNUMRECORDS = 10;
34
35 private void myInitComonents() {
36 try {
37 raf = new RandomAccessFile("another raf - 7.txt", "rw");
38
39 for (int i = 0; i < MAXNUMRECORDS * recordLength; i++) {
40
41 try {
42 raf.writeUTF("");
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;
323 System.out.println("initialPosition: " + initialPosition);
324
325
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);
339 raf.writeUTF(hashInputNameTextField.getText());
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);
372 }
373 } while (!foundKeyLookingFor);
374 raf.seek(pointer);
375 raf.readUTF();
376
377 hashDisplayNumberTextField.setText(raf.readUTF());
378
379
380
381 } catch (IOException ex) {
382 }
384 }