Results 1 to 16 of 16
  1. #1
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default 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;
    	}
    
    }
    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++;
    	}
    }
    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.

    I have included the file (ignore the GUI as I am trying to get the program working before completing it).
    Attached Files Attached Files

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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.

  3. #3
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default 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?

  4. #4
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default 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;
    	}
    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++;
    		}
    I fixed the error code but it incorrectly checks the words. Here is the output:
    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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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.

  6. #6
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Spell Checker using hash tables not working need help.

    Quote Originally Posted by Norm View Post
    if (isCorrect = false)
    This is an assignment(=), not a test for equality(==).

    Use:
    if(!isCorrect)
    if(isCorrect = false) wouldn't even compile, would it?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default Re: Spell Checker using hash tables not working need help.

    wouldn't even compile,
    Have you tried it?
    What data type does the expression return?

  8. #8
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Spell Checker using hash tables not working need help.

    Quote Originally Posted by Norm View Post
    Have you tried it?
    What data type does the expression return?
    Ha! It works because it evaluates to a boolean. I was confusing myself with

    if(i = k) where, i and k are integers, working in C, but not in java.

    Of course, C doesn't use the boolean type..

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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.

  10. #10
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Spell Checker using hash tables not working need help.

    Quote Originally Posted by Norm View Post
    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.
    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.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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. #12
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default Re: Spell Checker using hash tables not working need help.

    Quote Originally Posted by Norm View Post
    That's great. I always feel badly when I haven't checked out something before I post a question about some code not working.
    Be positive. Just learn. Don't feel bad! You are human.

    I checked, checked the wrong thing. It's human...we make mistakes..

    Learning point..

    Be more precise and dedicated in checking, even if you think something different is equivalent.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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!"

  14. #14
    2by4 is offline Banned
    Join Date
    Dec 2011
    Posts
    143
    Rep Power
    0

    Default 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!

  15. #15
    chris1 is offline Member
    Join Date
    Apr 2011
    Posts
    18
    Rep Power
    0

    Default 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.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,331
    Rep Power
    25

    Default 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

  1. Hash-tables IO
    By loopsnhoops in forum New To Java
    Replies: 8
    Last Post: 06-01-2011, 09:55 AM
  2. hash tables vs binary tree
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 12-16-2010, 10:25 PM
  3. Java Hash Tables
    By Growler in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 05:50 AM
  4. Spell Checker in Swing
    By dhaivat in forum New To Java
    Replies: 2
    Last Post: 07-01-2010, 05:08 PM
  5. Question about hash tables
    By behrk2 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 04:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •