I am trying to learn more about using HashMaps in java. I would like to write a program that searches through a list of jumbled words from one text file and returns the unscrambled word(s) from another text file. Any suggestions how to do this?

This will really depend on what you're trying to do.
A hashmap will be beneficial when you already know what things belong together (key, value pairs).

Will the program need to unscramble (or scramble) the words?

The program will need to unscrambled the word. So for instance the jumble list may contain addej, ahicryrhe, alvan, annaab, and baltoc. And I want the program to use a hashmap to unscramble the words to jaded, hierarchy, naval, banana, cobalt.

This will really depend on what you're trying to do.
A hashmap will be beneficial when you already know what things belong together (key, value pairs).

Will the program need to unscramble (or scramble) the words?

OK, so in what capacity do you want to use the hash table?
Do you want it to hold both the scrambled AND the unscrambled word, so when it is presented with a scrambled word, it will just return the "value" of the unscrambled word?
...or will you try to use it to figure out what letters belong in the unscrambled word?

A little of both I guess. So the scrambled word will be the key, and I want to use the hash map to figure out which letters belong in the word and then display the unscrambled word as the value. I want the output to look like anaban: banana.

OK, so in what capacity do you want to use the hash table?
Do you want it to hold both the scrambled AND the unscrambled word, so when it is presented with a scrambled word, it will just return the "value" of the unscrambled word?
...or will you try to use it to figure out what letters belong in the unscrambled word?

...so, something like this, but getting the values from a file?

import   java.util.*;
import   java.io.*;

class Hashtest
{
   public static void main(String[] args)
   {
      Hashtable<String, String> map_s2sData = new Hashtable<String, String>();
      map_s2sData.put("addej", "jaded");
      map_s2sData.put("ahicryrhe", "banana");
      map_s2sData.put("alvan", "naval");
      map_s2sData.put("baltoc", "cobalt");
      map_s2sData.put("anaban", "banana");


      if(map_s2sData.containsKey("anaban"))
      {
         System.out.println(map_s2sData.get("anaban"));
      }
   }
}

So say the file where my scrambled words are stored is called scrambled.txt and the file where the unscrambled words that I want to match them to is called unscrambled.txt. I want the hash map to read each "key" from the scrambled.txt file and match it to the "value" from the "unscrambled.txt" file and then display. How would that change the code?

...so, something like this, but getting the values from a file?

import   java.util.*;
import   java.io.*;

class Hashtest
{
   public static void main(String[] args)
   {
      Hashtable<String, String> map_s2sData = new Hashtable<String, String>();
      map_s2sData.put("addej", "jaded");
      map_s2sData.put("ahicryrhe", "banana");
      map_s2sData.put("alvan", "naval");
      map_s2sData.put("baltoc", "cobalt");
      map_s2sData.put("anaban", "banana");


      if(map_s2sData.containsKey("anaban"))
      {
         System.out.println(map_s2sData.get("anaban"));
      }
   }
}

Which technique do you use for reading files?

I tried to solve this problem before, but using an arrayList. You can see by this code how I kind of want my new program to run, but using a hashmap.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.Scanner;

public class Solver 
{
	public static void main (String[] args) 
	{
		ArrayList<String> dictKeys = new ArrayList<String>();
		File f = new File("dictionary.txt");
		Scanner infile = null;
		try
		{
			infile = new Scanner (f);
		}
		catch (FileNotFoundException e)
		{
			System.out.printf("File not found :");
		}
		while (infile.hasNext())
		{
			String token = infile.next();
			dictKeys.add(token);
		}
		Collections.sort(dictKeys);
		
		/*Iterator<String> it = dictKeys.iterator();
		while(it.hasNext()){
			System.out.println(it.next());
		}*/
		ArrayList<String> keys = new ArrayList<String>();
		//keys = dictKeys;
		for(int i=0; i<dictKeys.size(); i++)
		{
			String word = dictKeys.get(i);
			char[] content = word.toCharArray();
			Arrays.sort(content);
			String sorted = new String(content);
			keys.add(sorted);
		}
		
		
		/*for(int i=0; i<keys.size(); i++)
		{
			System.out.println(keys.get(i));
		}*/
		
		ArrayList<String> dictValues = new ArrayList<String>();
		Scanner keyboard = new Scanner(System.in);
		System.out.println("Enter jumbles file name: ");
		String jumbles = keyboard.nextLine();
		File g = new File(jumbles);
		try {
			infile = new Scanner (g);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		while(infile.hasNext())
		{
			String token = infile.next();
			dictValues.add(token);
		}
		
		Collections.sort(dictValues);
		
		for(int i=0; i<dictValues.size(); i++)
		{
			String jumble = dictValues.get(i);
			System.out.print(jumble + ":");
			//String jumbleCopy = jumble;
			String word = jumble;
			char [] content = word.toCharArray();
			Arrays.sort(content);
			String sorted = new String (content);
			String jumbleCopy = sorted;
			
			for(int j=0; j<keys.size(); j++)
			{
				if (jumbleCopy.equals(keys.get(j)))
				{
					System.out.print(" " + dictKeys.get(j) );
				}
			}
			System.out.println();
		}
		
	}

}

Which technique do you use for reading files?

I was assuming you already knew the filename to be entered, so you wouldn't need to ask the user. Also, I assumed the files were already in the order they needed to be. Sorting will make them lose their match-by-position.

Here's a test solution where I load the key file into a Vector<String> then load the value file and use the keys from the vector to load the Hashtable.
I could have done two simultaneous file reads, but I don't like that.

/*
This program reads two files -- one scrambled, one unscrambled and fills a
Hashtable based on the enumerated position of the data in the two files
*/
import   java.util.*;
import   java.io.*;

class FileToHash
{
   public static void main(String[] args)
   {
      Hashtable<String, String> map_s2sData = new Hashtable<String, String>();
      Vector<String> lst_strScrambled = new Vector<String>();
      String strData = "";
      String strFindMe = "anaban";

      try
      {
         BufferedReader fileIn = new BufferedReader(new FileReader("Scrambled.txt"));

         while(null != (strData = fileIn.readLine()))
         {
            lst_strScrambled.add(strData.trim());
         }

         fileIn.close();

         fileIn = new BufferedReader(new FileReader("Unscrambled.txt"));

         ////////////////////////////////////////////////////
         // Load the Hashtable with the data from both files
         int i = 0;
         while(null != (strData = fileIn.readLine()))
         {
            map_s2sData.put(lst_strScrambled.get(i++), strData.trim());
         }

         fileIn.close();
      }
      catch(Exception exc)
      {
         System.out.println("Exception: " + exc.getMessage());
         return;
      }

      if(map_s2sData.containsKey(strFindMe))
      {
         System.out.println(map_s2sData.get(strFindMe));
      }
   }
}

So, now I'm confused. Is the user going to have a number of round to try to decipher the word?

For the program above I had the user enter the jumbles.txt on the command line. This then triggered the program to process through the words, sorting them in alphabetical order, and returning their unscrambled words. I would like this program to work the same, but some how by using a hashmap. The user doesn't actually try to unscramble the word.

I was assuming you already knew the filename to be entered, so you wouldn't need to ask the user. Also, I assumed the files were already in the order they needed to be. Sorting will make them lose their match-by-position.

Here's a test solution where I load the key file into a Vector<String> then load the value file and use the keys from the vector to load the Hashtable.
I could have done two simultaneous file reads, but I don't like that.

/*
This program reads two files -- one scrambled, one unscrambled and fills a
Hashtable based on the enumerated position of the data in the two files
*/
import   java.util.*;
import   java.io.*;

class FileToHash
{
   public static void main(String[] args)
   {
      Hashtable<String, String> map_s2sData = new Hashtable<String, String>();
      Vector<String> lst_strScrambled = new Vector<String>();
      String strData = "";
      String strFindMe = "anaban";

      try
      {
         BufferedReader fileIn = new BufferedReader(new FileReader("Scrambled.txt"));

         while(null != (strData = fileIn.readLine()))
         {
            lst_strScrambled.add(strData.trim());
         }

         fileIn.close();

         fileIn = new BufferedReader(new FileReader("Unscrambled.txt"));

         ////////////////////////////////////////////////////
         // Load the Hashtable with the data from both files
         int i = 0;
         while(null != (strData = fileIn.readLine()))
         {
            map_s2sData.put(lst_strScrambled.get(i++), strData.trim());
         }

         fileIn.close();
      }
      catch(Exception exc)
      {
         System.out.println("Exception: " + exc.getMessage());
         return;
      }

      if(map_s2sData.containsKey(strFindMe))
      {
         System.out.println(map_s2sData.get(strFindMe));
      }
   }
}

So, now I'm confused. Is the user going to have a number of round to try to decipher the word?

So is this what you mean?
1. Write a method to sort the letters of a word and return the sorted word
2. Read a file of real words
3. For each real word call sort, then add the sorted word as key and the real word as value to a hashmap.
4. Read a file or stream of user-entered jumbled words
5. For each jumbled word call sort, then use the sorted jumbled word as key to retrieve the real word from the hashmap

1. Read the scrambled txt file and put those words in alphabetical order, but the words should still be scrambled (ex. addej, ahicryrhe, alvan,). In the hashmap these words should be in the left column as keys.
2. Then read the txt file of real unscrambled words. The real words should be the values in the right column of the hashmap.

So is this what you mean?
1. Write a method to sort the letters of a word and return the sorted word
2. Read a file of real words
3. For each real word call sort, then add the sorted word as key and the real word as value to a hashmap.
4. Read a file or stream of user-entered jumbled words
5. For each jumbled word call sort, then use the sorted jumbled word as key to retrieve the real word from the hashmap

OK, maybe I was trying to solve a more difficult problem, where you don't know the scrambled words in advance, but can find the unscrambled word from any scrambled version of it.
So, back to your version...
HashMaps are not in any defined order, it makes no sense to put the scrambled words in alphabetical order if you are just going to use them as keys in a hashmap. Also, if you change the order of the scrambled words by sorting them, how will you match them to the corresponding unscrambled version?

OK, I'll try to ask a simpler question:
Why would you sort scambled words?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.