Jump to content

Hashtable Comparison Algorithm

Featured Replies

Hi all,

 

Involved in many projects at the moment & I've had a mental block with this algorithm.

 

Here's the source code, I'm trying to compare the keys in my Hashtable and the values in the array and if the "words" appear in both data structures to produce a result, but the line for line algorithm is really messing with my head:

 

public void addBook(String filename) throws IOException

 

{

 

String[] linearray;

 

String line;

 

try

 

{

 

File b = new File(filename);

 

BufferedReader fileread = new BufferedReader(new FileReader(b));

 

in = new Scanner(b);

 

 

 

while(in.hasNextLine())

 

{

 

line = fileread.readLine(); /* Read in whatever line */

 

linearray = line.split("([.,!?:;'\"-]|\\s)+)"); /* this handles punctuation and whitespace and adds it to the array */

 

for(int i=0; i<=linearray.length; i++)

 

{

 

 

 

}

 

}

 

}

 

 

 

catch (FileNotFoundException e)

 

{

 

e.printStackTrace();

 

}

 

}

 

 

 

 

Any help you could provide would be great, I'm not looking for spoon fed source code, just some algorithms to help get my creative juices flowing!

 

 

 

 

Dan xx

 

 

for(int i=0; i<=linearray.length; i++){

// get word from array at first

String word = linearray;

//then use word to query the hashtable

Object o = targetHashTable.get(word);

//if the result is not null, it means there is a hit.

if(o!=null){

//product the result

}

}

Edited by alextui

  • Author

I'm getting a null pointer exception at the line with Object o on it :/

 

However I think it may be an error in my main method.. here's the source code but I'm not sure because all I'm doing is calling a method from the HashTable class & I have initialised that at the first line of the method.

 

package cs21120_assignment_data;

 

 

 

 

import java.io.*;

 

import java.util.Hashtable;

 

import java.util.Scanner;

 

 

 

 

import javax.swing.SwingUtilities;

 

 

 

 

public class Concordance {

 

 

 

 

/**

 

* @param args

 

* @throws IOException

 

*/

 

public static void main(String[] args) throws IOException

 

{

 

 

 

HashTable hashtable = new HashTable ();

 

 

 

hashtable.addBook("oliver.txt");

 

 

 

hashtable.readSourceFile("index.txt");

 

 

 

}

 

 

 

}

 

 

 

 

The source code for the HashTable is the same as above, but I'm not convinced it's a problem with main method even after reading the error message...

 

 

 

 

Exception in thread "main" java.lang.NullPointerException

 

at cs21120_assignment_data.HashTable.addBook(HashTable.java:86)

 

at cs21120_assignment_data.Concordance.main(Concordance.java:26)

 

 

 

 

 

 

 

 

 

 

 

  • 3 weeks later...

I don't know about your code, but I've built several optimal structures on Java as a library ...

 

They're in my blog .. Check them: Blog:Java

 

The first is optimal binary tree which is a hash structure of binary trees with O(log(N/K)), Blog:OBT

 

The second is Insta:Associative Array which is an associative array with O(1), Blog:Insta

 

good luck

Edited by khaled

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.