danieldrave Posted October 24, 2011 Share Posted October 24, 2011 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 Link to comment Share on other sites More sharing options...
alextui Posted October 25, 2011 Share Posted October 25, 2011 (edited) 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 October 25, 2011 by alextui Link to comment Share on other sites More sharing options...
danieldrave Posted October 27, 2011 Author Share Posted October 27, 2011 Thanks very much that really did help solve my problem! Marvellous xxx Link to comment Share on other sites More sharing options...
danieldrave Posted October 30, 2011 Author Share Posted October 30, 2011 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) Link to comment Share on other sites More sharing options...
khaled Posted November 14, 2011 Share Posted November 14, 2011 (edited) 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 November 14, 2011 by khaled Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now