Results 1 to 16 of 16
- 12-11-2011, 06:14 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Spell Checker using hash tables not working need help.
Java Code:import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class SpellChecker { public static void checkSpelling(String dictName, String docName) throws FileNotFoundException { System.out.println("This is the spell checker"); int i, N, W, index; String S; char ch; boolean isFound; i = 0; N = 0; W = 0; index = 0; File D = new File(dictName); Scanner dictFile = new Scanner(D); String line; while (dictFile.hasNextLine()) { line = dictFile.nextLine(); N++; } N = N * 2; while (isPrime(N) != true) { // Find size of dictionary hashTable N = N + 1; isPrime(N); } File d = new File(dictName); Scanner dFile = new Scanner(d); String line2; HashTable dictTable = new HashTable(N); while (dFile.hasNextLine()) { line2 = dFile.nextLine(); dictTable.Insert(line2); } String[] Words = new String[50]; File F = new File(docName); Scanner docFile = new Scanner(F); String result, line3, str; str = ""; result = ""; while (docFile.hasNextLine()) { line3 = docFile.nextLine(); result += line3 + "\n"; } while (i < result.length()) { if (result.charAt(i) != ' ') { ch = result.charAt(i); str = str + ch; Words[W] = str; } else { str = ""; W++; } i++; } i = 0; while (index < Words.length) { S = Words[index]; if (dictTable.Contains(S) == true) System.out.println("Spelled Correctly"); else System.out.println(S + " is spelled incorrectly"); index++; } } static boolean isPrime(int n) { if (n % 2 == 0) return false; else for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) return false; } return true; } }So the program reads in the dictionary and puts the words into the hash table. Then the document being spell checked is read into an array. The program should then go through the array and call the contains method of the hashTable class. It should return true if found in the hashtable and false otherwise. It was printing true and false but now the program doesn't even get that far. When it was printing true and false it was not working correctly. Right now I am lost and am getting extremely frustrated so any help that you can give would be extremely appreciated.Java Code:public class HashTable<T> implements HashTableInterface<T> { int numEntries, location, hashVal; boolean isFound; protected String[] hashTable; public HashTable() { hashTable = new String[101]; } public HashTable(int n) { hashTable = new String[n]; } public void Insert(String S) { location = HashValue(S); while (hashTable[location] != null) location = (location + 1) % hashTable.length; hashTable[location] = S; NumEntries(); } public int HashValue(String S) { int i, ch1, ch2, ch3; i = 0; hashVal = 0; i = S.length(); while (i > 0){ if(i - 3 >= 0){ // change to ascii value for extra credit ch1 = S.charAt(i - 1); ch2 = S.charAt(i - 2); ch3 = S.charAt(i - 3); System.out.println("ch1 " + ch1 + ", ch2 " + ch2 + ", ch3 " + ch3); hashVal = hashVal + (ch3 * 1024) + (ch2 * 32) + ch1; System.out.println("Chunk value: " + hashVal); } else if (i - 3 == -1){ ch1 = S.charAt(i - 1); ch2 = S.charAt(i - 2); hashVal = hashVal + (ch2 * 32) + ch1; System.out.println("Ch1: " + ch1 + "Ch2: " + ch2); System.out.println("Chunk value: " + hashVal); } else if (i - 3 == -2){ ch1 = S.charAt(i - 1); hashVal = hashVal + ch1; System.out.println("Ch1 " + ch1 + ", Chunk: " + hashVal); } i = i - 3; } return (hashVal % hashTable.length); } public boolean Contains(String S) { int location; location = HashValue(S); while(hashTable[location] != S){ location = (location + 1) % hashTable.length; if (hashTable[location] == S) isFound = true; isFound = false; } return isFound; } public int NumEntries() { return numEntries++; } }
I have included the file (ignore the GUI as I am trying to get the program working before completing it).
- 12-11-2011, 01:32 PM #2
Re: Spell Checker using hash tables not working need help.
Have you tried debugging the code? If you do not have an interactive debugger, you can use printlns to show the values of variables as they change and to show the logic flow.
The print outs should show you where the code is not doing what you want it to do.
For testing, include ALL the data in the program instead of requiring external files. Make the minimum sized set of data that shows the problem.
- 12-11-2011, 02:07 PM #3
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
result contains newline characters. These will appear in the Strings in Words[] since you are only checking for the space character when separating out the words. Is this what you intend?
- 12-11-2011, 05:46 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
Java Code:public boolean Contains(String S) { location = HashValue(S); while(location < hashTable.length){ if (S.equalsIgnoreCase(hashTable[location])) return true; location++; } return false; }I fixed the error code but it incorrectly checks the words. Here is the output:Java Code:System.out.println("checking spelling"); i = 0; while (i < Words.length && Words[i] != null) { S = Words[i]; isCorrect = dictTable.Contains(S); System.out.println(isCorrect); if (isCorrect = false) System.out.println(S + " are spelled incorrectly"); i++; }
true
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
So it is always returning false so I have narrowed it down to a problem in the contains method. Is there anything obviously wrong with the Contains method that you can see by just looking at it?
- 12-11-2011, 05:57 PM #5
Re: Spell Checker using hash tables not working need help.
Add a println to print out the values of the Strings that are being compared so you can visually see what is happening. It could be a problem with your data.
Be sure to add delimiters to the Strings to show if there are any extra characters in either of them.
S.o.p("var=" + var + "<");
if (isCorrect = false)
This is an assignment(=), not a test for equality(==).
Use:
if(!isCorrect)Last edited by Norm; 12-11-2011 at 05:59 PM.
- 12-11-2011, 07:12 PM #6
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-11-2011, 07:16 PM #7
Re: Spell Checker using hash tables not working need help.
Have you tried it?wouldn't even compile,
What data type does the expression return?
- 12-11-2011, 07:25 PM #8
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
- 12-11-2011, 07:27 PM #9
Re: Spell Checker using hash tables not working need help.
I keep a very simple java source file open in my IDE for posting short statements for quick compiles and executions so I can test before posting. Saves some egg on the face.
- 12-11-2011, 07:33 PM #10
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
My vanity is not such a problem, so I don't see it as "egg on my face" -- just another memorable way to learn.
P.S I did actual try it, but I used ints instead of booleans, lol, because testing a boolean like that was unnatural.
Learning to spot errors that you would normally never make yourself is an important competence, which is one reason I am practising here.Last edited by 2by4; 12-11-2011 at 07:39 PM.
- 12-11-2011, 07:34 PM #11
Re: Spell Checker using hash tables not working need help.
That's great. I always feel badly when I haven't checked out something before I post a question about some code not working.
- 12-11-2011, 07:45 PM #12
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
- 12-11-2011, 07:51 PM #13
Re: Spell Checker using hash tables not working need help.
Good advice. I've been programming for over 40 years and can't help myself from having the thought:
"I should have know that!"
- 12-11-2011, 07:59 PM #14
Banned
- Join Date
- Dec 2011
- Posts
- 143
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
The thought in my mind is..
"Great, x more bug fix assignments that could have taken 10 minutes each, will now stand out and take 1 minute".
Thank you for the exchange. Much more memorable than if I had been so precise in the first case!
- 12-11-2011, 09:18 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 18
- Rep Power
- 0
Re: Spell Checker using hash tables not working need help.
It wasn't working because the words in MyDocument.txt had spaces after them which was causing them to not match any words in dictionary. After several hours of being completely lost and frustrated it now works.
- 12-11-2011, 09:20 PM #16
Re: Spell Checker using hash tables not working need help.
You can see now that printing the data that you were using and testing could have shown you there was a problem. As I said in post#5:
It could be a problem with your data. When printing out the values of variables
Be sure to add delimiters to the Strings to show if there are any extra characters in either of them.
S.o.p("var=" + var + "<");
Similar Threads
-
Hash-tables IO
By loopsnhoops in forum New To JavaReplies: 8Last Post: 06-01-2011, 09:55 AM -
hash tables vs binary tree
By counterfox in forum New To JavaReplies: 1Last Post: 12-16-2010, 10:25 PM -
Java Hash Tables
By Growler in forum New To JavaReplies: 1Last Post: 10-31-2010, 05:50 AM -
Spell Checker in Swing
By dhaivat in forum New To JavaReplies: 2Last Post: 07-01-2010, 05:08 PM -
Question about hash tables
By behrk2 in forum New To JavaReplies: 2Last Post: 07-08-2008, 04:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks