• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps HashMap Population

try2learn

Lurker
Hello all, have been struggling with this and am rather frustrated with my lack of ability.
Basically I would like to populate my hashMap with words read in from a dictionary file. I have about seven different versions of this attempt.
Code:
package ie.gmit;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;

public class DictionaryMap extends Activity {

	
	Map wordMap= new HashMap();
	
	public void load() throws Exception{
		InputStream is = getResources().openRawResource(R.raw.dictionary);
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF8")); 

			String next;
			while ((next = br.readLine()) != null) { 
				String word=next;
				int wlength=word.length();
				wordMap.put(wlength, word); 
				
				//Collection wCollection=wordMap.values();
				
				//Iterator wordItr=wCollection.iterator();
			}

		} catch (Exception e) {
			throw new Exception("[ERROR] Encountered a problem reading the dictionary. " + e.getMessage());		
		}
		((DictionaryMap) wordMap).getWordMap();
	}
	public int size(){
		return wordMap.size();
	}
	
	public Map getWordMap(){
		return wordMap;
	}
}

I have tried to search that hashmap but am unsure how far the program is getting if anywhere at all.

Code:
package ie.gmit;

import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class WordSearch extends Activity {
	
	@Override
	public void onCreate(Bundle savedInstanceState)  {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		try {
			init();
		} catch (Exception e) {
			//aaaaaaaaahhhh
		}
	}
	
	public void init() throws Exception{
		
		DictionaryMap dictionary = new DictionaryMap();
		dictionary.load();
		//Toast.makeText(getApplicationContext(), "init started", Toast.LENGTH_LONG).show();
		
		final Map wordMap=dictionary.getWordMap();
		
		final String searchTerm="@+id/txtsearchClue";
		final int searchTermLength=searchTerm.length();
		Button btnSearch=null;
		
		btnSearch = (Button)  findViewById(R.id.btnSearch);
		btnSearch.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	        	if(wordMap.containsKey(searchTermLength)){
	        		if(wordMap.containsValue(searchTerm)){
	        			Toast.makeText(getApplicationContext(), "Found", Toast.LENGTH_LONG).show();
	        		}
	        	}else
	        		Toast.makeText(getApplicationContext(), "key not Found", Toast.LENGTH_LONG).show();
	        	
	        }
		});
	}

}

Is this even the right way to go about setting up my map?
any help would be much appreciated.
mike
 
I took a quick gander and didn't see anything directly wrong. I suggest it would help alot if you didn't try catch the init(); call in your activity. This way the application will force close and your LogCat will show you a stack trace of exactly what went wrong. If you post that stack trace here I'll be willing to take a more detailed look at it.
 
Back
Top Bottom