Results 1 to 1 of 1
Thread: hashmap or search problem
- 04-09-2011, 01:55 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 1
- Rep Power
- 0
hashmap or search problem
hi
ive been stuck on my project for a while.. when i search the hashmap which contains words read in from a dict i never find anything it goes to the else in the if(map.containsKey(ss)){ test which means not found i eclipse is promping for a local var for wordmap which is why i suspect i should merge the classes. I think my design may be flawed? maybe i should merge the solver and dict classes? or maybe its the hashmap which is at fault? please any comments/suggestions are welcome...
thanks
flushdabuffer
Java Code:import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; public class CrosswordSolver extends Activity { private ListView foundWords = null; private ArrayAdapter<String> adapter; private EditText editText; private Button btnQuit = null; private Button btnSearch = null; Dictionary dictionary = new Dictionary(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ foundWords = (ListView) findViewById(R.id.listMatches); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item); foundWords.setAdapter(adapter); editText = (EditText) findViewById(R.id.txtSearch); btnQuit = (Button) findViewById(R.id.btnQuit); btnSearch = (Button) findViewById(R.id.btnSearch); btnQuit.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast.makeText(getBaseContext(), "You clicked the Quit button. Bye!", Toast.LENGTH_SHORT).show(); finish(); } }); btnSearch.setOnClickListener(new View.OnClickListener(){ public void onClick(View v) { //Dictionary dictionary = new Dictionary(); //Instantiate the dictionary try { dictionary.load(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //Load the dictionary into memory //Intent i = new Intent(CrosswordSolver.this, Dictionary.class); String searchTerm = editText.getText().toString(); Map<KeySearch, Word> map = dictionary.getWordMap(); //Get a hash map of the words KeySearch ss = new KeySearch(searchTerm); //Wrap the search term in our custom object if (map.containsKey(ss)){ //Check if key exists in hash table. This is an O(1) operation Word word = wordMap.get(word.hashCode()).add(next); //Get the values associated with the key in the hash table. wordMap.get(word.hashCode()).add(next); Toast.makeText(getBaseContext(), "You found key!", Toast.LENGTH_SHORT).show();//foundWords=( word.getWord()); //Print result } else{//Word is not in the hash table Toast.makeText(getBaseContext(), (searchTerm + " is not in the dictionary"),Toast.LENGTH_SHORT).show(); } finish(); } }); } }Java Code:import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* The Dictionary class has been altered to provide a choice of two data structures to use * for a word search - an array list and a hash table. */ public class Dictionary { private Map<KeySearch, Word> wordMap = new HashMap<KeySearch, Word>(); //Instance variable of type Map private List<Word> wordList = new ArrayList<Word>(); private final String DICTIONARY_FILE = "dictionary.png"; //A string instance variable public void load() throws Exception{ //If anything goes wrong, throw the exception to the calling method. Very lazy indeed! try { FileInputStream txtInput = new FileInputStream(DICTIONARY_FILE); //Wrap the file name in an input stream DataInputStream in = new DataInputStream(txtInput); //Allows us to read primitive data types (ints, chars, floats) from a stream BufferedReader br = new BufferedReader(new InputStreamReader(txtInput, "UTF8")); //Buffers the data input stream String next; while ((next = br.readLine()) != null) { //Loop through each line in the dictionary file Word word = new Word(next); //Create a new Word object using the next word in the dictionary wordMap.put(new KeySearch(next), word); //Also add the word to the hash map } in.close(); //Good manners to close any in/out streams. } catch (Exception e) { throw new Exception("[ERROR] Encountered a problem reading the dictionary. " + e.getMessage()); } } public List<Word> words(){ return this.wordList; } public int size(){ return wordList.size(); } public Word[] getSortedWords(){ return (Word[]) wordList.toArray(new Word[wordList.size()]); } public Map<KeySearch, Word> getWordMap(){ return wordMap; } }Java Code:import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /* The Dictionary class has been altered to provide a choice of two data structures to use * for a word search - an array list and a hash table. */ public class Dictionary { private Map<KeySearch, Word> wordMap = new HashMap<KeySearch, Word>(); //Instance variable of type Map private List<Word> wordList = new ArrayList<Word>(); private final String DICTIONARY_FILE = "dictionary.png"; //A string instance variable public void load() throws Exception{ //If anything goes wrong, throw the exception to the calling method. Very lazy indeed! try { FileInputStream txtInput = new FileInputStream(DICTIONARY_FILE); //Wrap the file name in an input stream DataInputStream in = new DataInputStream(txtInput); //Allows us to read primitive data types (ints, chars, floats) from a stream BufferedReader br = new BufferedReader(new InputStreamReader(txtInput, "UTF8")); //Buffers the data input stream String next; while ((next = br.readLine()) != null) { //Loop through each line in the dictionary file Word word = new Word(next); //Create a new Word object using the next word in the dictionary wordMap.put(new KeySearch(next), word); //Also add the word to the hash map } in.close(); //Good manners to close any in/out streams. } catch (Exception e) { throw new Exception("[ERROR] Encountered a problem reading the dictionary. " + e.getMessage()); } } public List<Word> words(){ return this.wordList; } public int size(){ return wordList.size(); } public Word[] getSortedWords(){ return (Word[]) wordList.toArray(new Word[wordList.size()]); } public Map<KeySearch, Word> getWordMap(){ return wordMap; } }Java Code:public class Word { private String word; public Word(String word) { this.word = word; } public String getWord() { return word; } public void setWord(String word) { this.word = word; } }
Similar Threads
-
hashmap problem
By minotaurus in forum Advanced JavaReplies: 5Last Post: 03-16-2011, 11:24 AM -
Problem with HashMap
By maz09 in forum New To JavaReplies: 2Last Post: 04-14-2010, 09:40 PM -
problem with removing element from HashMap
By checho in forum New To JavaReplies: 15Last Post: 01-10-2010, 01:10 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
Iterating through HashMap problem
By JordashTalon in forum New To JavaReplies: 1Last Post: 01-28-2009, 11:28 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks