Results 1 to 2 of 2
  1. #1
    theBurgh22 is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Having trouble understanding how to use Hash Tables

    I have to write a program that takes a scrambled version of a word (given by the user). The program should find all permutations of the input, which I have figured out.

    Then it should search through the permutations, growing
    them from left-to-right from the remaining characters, and prunes the
    search when it generates an impossible prefix. Prefixes and words are
    maintained in a hashed existence table for fast lookup.

    I have never used hash tables before and I don't understand how to use them here. If anyone could explain how I could go about that and maybe give an example, it would be greatly appreciated.

    Main Program
    Java Code:
    import java.util.*;
    import java.util.Scanner;
    import java.io.*;
    
    public class Wordgram {
    	public static void main (String[] args) throws IOException {
    		String fileName;
    		String thisLine;
    		String thisWord;
    		Scanner fileScanner;
    		Scanner inScan;
    		StringTokenizer words;
    		
    		
    		// Start program, get user input to unscramble
    		String input;
    		boolean repeat = false;
    		inScan = new Scanner(System.in);
    		System.out.println("Welcome to the Wordgram Program!");
    		do {
    			System.out.println("Enter wordgram: ");
    			input = inScan.next();
    			permutation(input);
    			repeat = true;
    		} while (repeat=true);
    	
    		
    		// Open the file and zero the line-counter:
    		fileName = args[0];
    		fileScanner = new Scanner(new File(fileName));
    
    		// Read lines while the file "hasNext()" line:
    		while (fileScanner.hasNext()) {
    			thisLine = fileScanner.nextLine();
    			// Create a tokenizer for the line and break off words while it "hasMoreTokens()":
    			words= new StringTokenizer(thisLine);
    			while (words.hasMoreTokens()) {
    				//System.out.println(words.nextToken());
    			}
    		}
    	}
    	public static void permutation(String s) {
    		permutation("",s);
    	}
    		
    	public static void permutation(String prefix,String remaining) {
    		int len = remaining.length();
    		if(len == 0)
    			System.out.println(prefix);
    		else {
    			for(int i=0; i < len; i++){
    				permutation(prefix + remaining.charAt(i), remaining.substring(0,i) + remaining.substring(i+1,len));
    			}
    		}
    	}
    }
    Hash Tables
    Java Code:
    import java.util.*;
    import java.util.Scanner;
    import java.io.*;
    
    public class StringTable {
    	
    }

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Hash tables in Java are encapsulated in the HashMap class. A HashMap is a collection of key/value pairs. They keys and the values are Objects, and they can be of any type you specify. You should use the generic form, and I believe you're going to want a HashMap<String, ArrayList<String>>. This means you're going to have a set of String keys, each of which is associated with an ArrayList of Strings.

    Take a look at the HashMap documentation, and experiment by writing a few simple programs that use HashMaps before tackling your current project.

    HashMap (Java Platform SE 6)

    Once you've stored objects in a HashTable, you can retrieve them very quickly if you have the key. It's not a difficult class to use, but you should get some practice with it.

    -Gary-

Similar Threads

  1. hash tables vs binary tree
    By counterfox in forum New To Java
    Replies: 1
    Last Post: 12-16-2010, 10:25 PM
  2. Java Hash Tables
    By Growler in forum New To Java
    Replies: 1
    Last Post: 10-31-2010, 05:50 AM
  3. trouble understanding code help
    By yasmin k in forum New To Java
    Replies: 4
    Last Post: 11-16-2009, 09:46 PM
  4. I have trouble understanding this program!
    By PureAwesomeness in forum New To Java
    Replies: 1
    Last Post: 03-16-2009, 05:34 AM
  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
  •