Results 1 to 17 of 17
  1. #1
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Working with HashMap

    Hello! I am working on a T9 prediction program and I could use some help.

    I am currently trying to convert inputted numbers into all possible combinations of the letters from a HashMap in the numbers2letters method (which needs to be changed). I currently have all of the numbers and letters mapped to each other now I just need some advice as to the best way to retrieve the possible combinations without a bunch of if statements.

    Any help is greatly appreciated!

    Thank you!

    Here is my code:

    Java Code:
    public class Keypad {
    
    	final HashMap<String, String> let2numMap = new HashMap<String, String>();
    
    	public static void main(String[] args) {
    		Keypad pad = new Keypad();
    		pad.fillMap();
    		pad.letters2numbers("1800CALLSAM");
    
    	}
    
    	public void letters2numbers(String letters){
    		StringBuffer sb = new StringBuffer();
    		letters = letters.toLowerCase();
    
    		for (int i = 0; i < letters.length(); i++) {
    			char input = letters.charAt(i);
    			String something = let2numMap.get(input + "");
    			sb.append(something);	
    		}
    		System.out.println("After converting from letters to numbers, your result is: " +sb);
    	}
    	
    	public String[] numbers2letters(String numbers){
    		return null;
    	}
    	
    	public String[] numbers2words(String numbers){
    		return null;
    	}
    
    	private void fillMap() {
    
    		let2numMap.put("0", "0");
    		let2numMap.put("1", "1");
    		let2numMap.put("2", "2");
    		let2numMap.put("3", "3");
    		let2numMap.put("4", "4");
    		let2numMap.put("5", "5");
    		let2numMap.put("6", "6");
    		let2numMap.put("7", "7");
    		let2numMap.put("8", "8");
    		let2numMap.put("9", "9");
    		let2numMap.put("*", "*");
    		let2numMap.put("#", "#");
    		let2numMap.put("a", "2");
    		let2numMap.put("b", "2");
    		let2numMap.put("c", "2");
    		let2numMap.put("d", "3");
    		let2numMap.put("e", "3");
    		let2numMap.put("f", "3");
    		let2numMap.put("g", "4");
    		let2numMap.put("h", "4");
    		let2numMap.put("i", "4");
    		let2numMap.put("j", "5");
    		let2numMap.put("k", "5");
    		let2numMap.put("l", "5");
    		let2numMap.put("m", "6");
    		let2numMap.put("n", "6");
    		let2numMap.put("o", "6");
    		let2numMap.put("p", "7");
    		let2numMap.put("q", "7");
    		let2numMap.put("r", "7");
    		let2numMap.put("s", "7");
    		let2numMap.put("t", "8");
    		let2numMap.put("u", "8");
    		let2numMap.put("v", "8");
    		let2numMap.put("w", "9");
    		let2numMap.put("x", "9");
    		let2numMap.put("y", "9");
    		let2numMap.put("z", "9");
    	}
    }
    Last edited by Norm; 01-24-2012 at 02:55 AM. Reason: added code tags

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    way to retrieve the possible combinations
    Can you give some examples of what you are trying to do?

    convert inputted numbers into all possible combinations of the letters
    I don't see how a Hashmap is useful here.

    Is this what you want: Given a number you want the 3 or 4 letters associated with it.
    Last edited by Norm; 01-24-2012 at 03:01 AM.

  3. #3
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    Yes, I am wanting to enter a set of numbers. An example would be that if I put in the string of numbers "733", it would return all 36 combinations, such as "pdd". Do you think that an Array[] would work better?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    Given a number you want all the letters associated with it.
    Your Hashmap is backwards for me. Have the numbers be the key and the value be an array(list???) of the letters associated with that number.

  5. #5
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    Ok, So that was a good point. I tried adjusting it, and now I am getting this when I run it:

    After converting from letters to numbers, your result is: [Ljava.lang.String;@617df472[Ljava.lang.String;@143b9a5f[Ljava.lang.String;@5513dd59[Ljava.lang.String;@5513dd59nullnullnullnullnullnul lnull

    My new code is this:
    Java Code:
    public class Keypad {
    
    	final HashMap<String, String[]> let2numMap = new HashMap<String, String[]>();
    	
    	public static void main(String[] args) {
    		Keypad pad = new Keypad();
    		pad.fillMap();
    		pad.letters2numbers("1800CALLSAM");
    
    	}
    
    	public void letters2numbers(String letters){
    		StringBuffer sb = new StringBuffer();
    		letters = letters.toLowerCase();
    
    		for (int i = 0; i < letters.length(); i++) {
    			char input = letters.charAt(i);
    			String[] something = let2numMap.get(input + "");
    			sb.append(something);	
    		}
    		System.out.println("After converting from letters to numbers, your result is: " +sb);
    	}
    	
    	public String[] numbers2letters(String numbers){
    		return null;
    	}
    	
    	public String[] numbers2words(String numbers){
    		return null;
    	}
    
    	private void fillMap() {
    		
    		String[] array0 = new String[1];
    	    array0[0] = "0";
    		let2numMap.put("0", array0);
    		
    		String[] array1 = new String[1];
    	    array1[0] = "1";
    		let2numMap.put("1", array1);
    		
    		String[] array2 = new String[4];
    	    array2[0] = "2";
    	    array2[1] = "a";
    	    array2[2] = "b"; 
    	    array2[3] = "c";
    		let2numMap.put("2", array2);
    	    
    	    String[] array3 = new String[4];
    	    array3[0] = "3";
    	    array3[1] = "d";
    	    array3[2] = "e"; 
    	    array3[3] = "f";
    	    let2numMap.put("3", array3);
    	    
    	    String[] array4 = new String[4];
    	    array4[0] = "4";
    	    array4[1] = "g";
    	    array4[2] = "h"; 
    	    array4[3] = "i";
    	    let2numMap.put("4", array4);
    	    
    	    String[] array5 = new String[4];
    	    array5[0] = "5";
    	    array5[1] = "j";
    	    array5[2] = "k"; 
    	    array5[3] = "l";
    	    let2numMap.put("5", array5);
    	    
    	    String[] array6 = new String[4];
    	    array6[0] = "6";
    	    array6[1] = "m";
    	    array6[2] = "n"; 
    	    array6[3] = "o";
    	    let2numMap.put("6", array6);
    	    
    	    String[] array7 = new String[5];
    	    array7[0] = "7";
    	    array7[1] = "p";
    	    array7[2] = "q"; 
    	    array7[3] = "r";
    	    array7[4] = "s";
    	    let2numMap.put("7", array7);
    	    
    	    String[] array8 = new String[5];
    	    array8[0] = "8";
    	    array8[1] = "t";
    	    array8[2] = "u"; 
    	    array8[3] = "v";
    	    let2numMap.put("8", array8);
    	    
    	    String[] array9 = new String[5];
    	    array9[0] = "9";
    	    array9[1] = "w";
    	    array9[2] = "x"; 
    	    array9[3] = "y";
    	    array9[4] = "z";
    	    let2numMap.put("9", array9);
    	    
    	    String[] arrayAsterick = new String[1];
    	    arrayAsterick[0] = "*";
    		let2numMap.put("*", arrayAsterick);
    		
    		String[] arrayPound = new String[1];
    	    arrayPound[0] = "#";
    		let2numMap.put("#", arrayPound);
    
    	}
    }
    Thank you for all of your help!
    Last edited by Norm; 01-24-2012 at 01:36 PM. Reason: added code tags

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    [Ljava.lang.String;@617df472
    That String is what is returned by the default toString method for an array of Strings. If you want the contents of the String array use the Arrays class's toString() method or get one of the elements of the array by using array notation: [index]

    I'm confused about what you want to do.
    The method name: letters2numbers seems to say you have letters and want to get the numbers.
    I asked: Given a number you want all the letters associated with it.

    Which is it?
    Last edited by Norm; 01-24-2012 at 01:42 PM.

  7. #7
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    I'm sorry I didn't clarify enough. I am trying to get the numbers2letters() method to work. When I had posted the last post, the entire program was not working. However, I fixed the letters2numbers so it is working properly now.

    My question is would you have any ideas for a method that would take a given number, find the ArrayList associated with that number, and then print out the contents of the ArrayList? I'm hoping to have it find that ArrayList, and then return the first letter. Then, I would like it to go to the next number and return it's first letter from that ArrayList. Does that make more sense? I know I have to have some nested loop implementation, I'm just trying to figure out the details.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    take a given number, find the ArrayList associated with that number
    That sounds like a Map. The key=the number, the value is the ArrayList

    Can you describe what you what the application to do? Your question is about a technique you want to use in the program. Perhaps there is a better way to solve the problem.

  9. #9
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    Yes! That's what I have implemented now. I know I have to get the number from the HashMap (called betterMap in my program currently). After a for loop that looks like this:

    for(int i = 0; i < numbers.length(); i++){
    I need something that will get the second index of the ArrayList and return that.
    }

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    I need something that will get the second index of the ArrayList and return that.
    Read the API doc for the ArrayList class. It has methods for getting to its contents.

  11. #11
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    I've looked through that, and I know that I will eventually use the .get() method for retrieving the specific index.

    Now, if you don't mind, what do I put to retrieve the value of the number (which is the ArrayList) so I can call the get method on it? ArrayList.get(2);

    I'm confused as to what I would need to get in order access the value of that mapping. I know I could hard code array2.get(2), but I want it to get the value of the mapping and call the get method on that.

    Sorry this is taking so long!

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Working with HashMap

    yourMap.get(1) will give you the array list associated with the number 1 (presuming yourMap is HashMap<Integer, ArrayList<String>>).

  13. #13
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    Well....so I've tried working with that, but now I am getting null pointer exception....?

    Here is the code:
    public class Keypad {

    final HashMap<String, String> let2numMap = new HashMap<String, String>();
    final HashMap<String, ArrayList<String>> betterMap = new HashMap<String, ArrayList<String>>();
    public static void main(String[] args) {
    Keypad pad = new Keypad();
    pad.fillLetterMap();
    pad.fillNumMap();
    pad.letters2numbers("1800CALLSAM");
    pad.numbers2letters("18002662278");

    }
    public void numbers2letters(String number){
    ArrayList<String> temp = new ArrayList<String>();
    betterMap.get(number).get(2);
    for(int i = 0; i < number.length(); i++) {
    temp.add(number);
    }
    System.out.println("After converting from numbers to letters, your options are: " + number);
    }

    and the betterMap's fill method looks like:

    private void fillNumMap() {
    ArrayList<String> array0 = new ArrayList<String>();
    array0.add("0");
    betterMap.put("0", array0);
    ArrayList<String> array1 = new ArrayList<String>();
    array1.add("1");
    betterMap.put("1", array1);
    ArrayList<String> array2 = new ArrayList<String>();
    array2.add("2");
    array2.add("a");
    array2.add("b");
    array2.add("c");
    betterMap.put("2", array2);

    ArrayList<String> array3 = new ArrayList<String>();
    array3.add("3");
    array3.add("d");
    array3.add("e");
    array3.add("f");
    betterMap.put("3", array3);


    ArrayList<String> array4 = new ArrayList<String>();
    array4.add("4");
    array4.add("g");
    array4.add("h");
    array4.add("i");
    betterMap.put("4", array4);


    ArrayList<String> array5 = new ArrayList<String>();
    array5.add("5");
    array5.add("j");
    array5.add("k");
    array5.add("l");
    betterMap.put("5", array5);

    ArrayList<String> array6 = new ArrayList<String>();
    array6.add("6");
    array6.add("m");
    array6.add("n");
    array6.add("o");
    betterMap.put("6", array6);

    ArrayList<String> array7 = new ArrayList<String>();
    array7.add("7");
    array7.add("p");
    array7.add("q");
    array7.add("r");
    array7.add("s");
    betterMap.put("7", array7);

    ArrayList<String> array8 = new ArrayList<String>();
    array8.add("8");
    array8.add("t");
    array8.add("u");
    array8.add("v");
    betterMap.put("8", array8);

    ArrayList<String> array9 = new ArrayList<String>();
    array9.add("9");
    array9.add("w");
    array9.add("x");
    array9.add("y");
    array9.add("z");
    betterMap.put("9", array9);

    ArrayList<String> arrayAsterisk = new ArrayList<String>();
    arrayAsterisk.add("*");
    betterMap.put("*", arrayAsterisk);
    ArrayList<String> arrayPound = new ArrayList<String>();
    arrayPound.add("#");
    betterMap.put("#", arrayPound);

    }

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    I am getting null pointer exception
    Please post the full text of the error message. It has important information about the exception.

  15. #15
    Huntersgirl12 is offline Member
    Join Date
    Jan 2012
    Posts
    11
    Rep Power
    0

    Default Re: Working with HashMap

    Exception in thread "main" java.lang.NullPointerException
    at Keypad.numbers2letters(Keypad.java:32)
    at Keypad.main(Keypad.java:14)

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,398
    Rep Power
    25

    Default Re: Working with HashMap

    Look at line 32 in Keypad and see what variable has a null value. Then backtrack to see why that variable does not have a valid value.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Working with HashMap

    As a help, the API for HashMap.get() states:
    "Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key."

Similar Threads

  1. HashMap
    By Shukaib_khan in forum New To Java
    Replies: 4
    Last Post: 12-25-2011, 12:51 PM
  2. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  3. Replies: 2
    Last Post: 01-04-2011, 04:30 AM
  4. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  5. Replies: 8
    Last Post: 05-28-2008, 07:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •