Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help With Java Hash Table Project

  1. #1
    Junior Member
    Join Date
    Nov 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help With Java Hash Table Project

    Hello everyone, so I have a project here that's kicking my butt a bit and I was seeing if I could get any ideas from the outside. This project is to construct a simple spell check given a dictionary list, and it will check a word from a given string that is spelled wrong. Currently we need to test the word "mam" which should be man, however when we run it through our program it matches with "all" as the correct word, which just isn't the case. We are required to use hash tables which makes this project even more so annoying and I'm having problems. Other spelling errors such as "teh" seem to come up with the correct word but the repeating letters in mam are messing with it. Any suggestions on how I could efficiently put this together to give the correct word? Or any other suggestions at all would be great since I'm not too great at this yet.

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map.Entry;
    import java.util.Map;
    import java.util.Iterator;
     
    /**
     *
     * @author denni
     */
     
    public class ACO201project3DSE {
     
        public static void main(String[] args) {
     
     
              char[] characters = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
     
                      'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
     
                      'w', 'x', 'y', 'z', ' ' };
     
            String inputList = "mam";
     
             String dictionaryList = "a about again all an and are as at be but by " +
            "can " +"do " +"down " +"first " +"for " +"from " +"good " +"have " +"he " +
            "her " +"here " +"him " +"his " +"I " +"if " +"in " +"into " +"is " +"it " +"just " +
            "like " +"listen " +"little " +"make " +"man " +"many " +"may " +"more " +"most " +"my "
            +"near " +"no " +"not " +"now " +"of " +"on " +"one " +"only " +"or " +
            "other " +"out " +"people " +"please " +"program " +"read " +"said " +"say " +"see " +"she " +
            "should " +"slow " +"small " +"so " +"some " +"stop " +"than " +"that " +
            "the " +"then " +"there " +"they " +"this " +"through " +"to " +"tomorrow " +
            "true " +"two " +"up " +"use " +"very " +"water " +"was " +"way " +"we " +
            "were " +"what " +"when " +"where " +"which " +"while " +"who " +"will " +
            "with " +"would " +"write " +"yes " +"yesterday " +"you ";
     
             //HashSet<String> dictionaryArray = new HashSet<String>();
     
     
     
     
            //********************************************************************************  
            String[] inputCompare = inputList.split(" ");
            String[] dictionaryArray = dictionaryList.split(" ");
            String possibleWords = "";
            String output = "";
     
            //********************************************************************************
            System.out.println("input string words separated by whitespace: "
                + inputList);
            System.out.println("output string: " + Arrays.toString(inputCompare));
     
            //********************************************************************************
            for(int i = 0; i < inputCompare.length; i++)
            {
                //********************************************************************************
                int j = 0;
     
                    while(!inputCompare[i].equals(dictionaryArray[j]))
                    {
     
                        j++;
                        if(j >= 100)
                        {
                            System.out.println(" /////////// NO MATCH FOUND ******************* ");
     
                            //********************************************************************************
                            String checkWord = inputCompare[i];
                            System.out.println("Check word is: " + checkWord);
     
                            System.out.println(freqCount(checkWord));
     
                           String[] lengthCompareInput = freqCount(checkWord).split(" ");
                           System.out.println(lengthCompareInput[0]);
     
                           for(int q = 0; q < dictionaryArray.length; q++)
                           {
                               String[] lengthCompareDict = freqCount(dictionaryArray[q]).split(" ");
     
                               if(lengthCompareInput[0].equals(lengthCompareDict[0]) && lengthCompareInput[1].charAt(0) == lengthCompareDict[1].charAt(0))
                               {
     
                                   //while()
                                   System.out.println("Match at: " + dictionaryArray[q]);
     
                               }
                           }
     
                          // if(lengthCompareInput[0].equals(dictionaryArray)
     
     
     
                            //System.out.println(total);
     
                            //HashSet<String> dictCheck = new HashSet<>();
                           // dictCheck.add(dictionaryList);
     
                            //System.out.println("Hash map contains: " + dictCheck.contains(checkWord));
     
                            //for(int k = 0; k < checkWord.length();k++)
                           // { //CHECK EACH LETTER OF THE INPUT WORD
     
                               //int n = 0;
                               // while(checkWord.charAt(k)!=characters[n])
                                //{
                                  //  n++;
                                //}
     
                                //System.out.println("Char is: " + characters[n]);
     
                            //}
                            //j = 0;
                            //i++;
     
                        }
                    }
     
                    //********************************************************************************
                    if(inputCompare[i].equals(dictionaryArray[j]))
                        {
                            //System.out.println("MATCH FOUND ******************* ");
                            output += inputCompare[i] + " ";
                            //System.out.println("MATCH FOUND: " + output);
                            //System.out.println("MATCH FOUND ******************* ");
                        }
                    //********************************************************************************
            }
            //********************************************************************************
            //System.out.println("FINAL OUTPUT: " + output);  
            System.out.println(output);
    //********************************************************************************
        }
     
         static String freqCount(String input) {
     
           Map<Character, Integer> hashCount = new HashMap<>();
           Character c;
           for (int i = 0; i < input.length(); i++) {
               c = input.charAt(i);
               if (hashCount.get(c) != null) {
                   hashCount.put(c, hashCount.get(c) + 1);
               } else {
                   hashCount.put(c, 1);
               }
           }
           Iterator it = hashCount.entrySet().iterator();
           String freq= "";
           String letter = "";
           String count = "";
     
           while (it.hasNext()) {
               Map.Entry pairs = (Map.Entry) it.next();
               letter += pairs.getKey();
               freq += pairs.getValue();
     
               it.remove();
           }
     
           count = freq + " " + letter;
     
           return count;
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help With Java Hash Table Project

    need to test the word "mam" which should be man,
    Can you explain in English how you find the word man given the input mam?

    I don't see any comments in the code that explains the algorithm it is using.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Implementing own generic hash table contains method
    By pyler in forum Java Theory & Questions
    Replies: 1
    Last Post: December 6th, 2013, 06:23 AM
  2. Having trouble implementing a hash table
    By EatSleepProgram in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 1st, 2013, 03:49 AM
  3. Summing values in hash table
    By akerman in forum Java Theory & Questions
    Replies: 9
    Last Post: November 13th, 2013, 02:55 PM
  4. Dictionary using Distributed Hash Table and BST Tree
    By dezett in forum What's Wrong With My Code?
    Replies: 28
    Last Post: June 23rd, 2012, 12:03 PM
  5. hash table probes
    By victorh in forum Collections and Generics
    Replies: 4
    Last Post: November 8th, 2011, 12:33 AM

Tags for this Thread