Page 1 of 1

## 11 Replies - 5574 Views - Last Post: 06 May 2011 - 02:20 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=205479&amp;s=a094cccef83b96ee0bdcee7040b450de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 skyerz

Reputation: 4
• Posts: 211
• Joined: 14-October 10

Posted 15 December 2010 - 06:58 AM

ok lets say i have a list of words in the alphabet, isit possible to assign the letter with the desingated number such as 2 = abc 3 = def and print out text such as "43556" to print "hello" . i understand that i have to read in the file with the words that i have however the logic into assigning the words to each number is bugging me. i wll be using a dictionary search list to obtain the letters needed

This post has been edited by skyerz: 15 December 2010 - 07:00 AM

Is This A Good Question/Topic? 0

### #2 skyerz

Reputation: 4
• Posts: 211
• Joined: 14-October 10

Posted 15 December 2010 - 07:20 AM

using a Hash map which woudl be easier how would im i able to find the letter "a" if i specify the number "2"

### #3 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

Posted 15 December 2010 - 02:04 PM

You can have a HashMap<Integer, ArrayList<Character>>. That way, each number corresponds to 3 or 4 letters. You add the letters to the ArrayList and then set that ArrayList as the value of an Integral key.

### #4 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

Posted 15 December 2010 - 06:00 PM

Or easier just an array of String containing the numbers

String[] phone = {"0", "1", "2ABC", ..., "9WXYZ"};

then you can use the String method indexOf() that takes a char or a String as argument to find if it matches

### #5 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

Posted 15 December 2010 - 06:32 PM

That is certainly a different way to do it...your way produces much smaller code. I know though, they taught in my AP Computer Science that if you can use a high level data structure effectively, that it is better.

On a practicality level, your design is better. On a theoretical scale...mine might win. Certainly interesting seeing the difference.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

Posted 15 December 2010 - 06:35 PM

In this case I am looking at the simplicity of code for a newbie (who probably does not know what a Hashmap is).
We do not have a switch that has to perform a thousand operations a second unless the OP has already a contract with AT&T

### #7 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11449
• Posts: 43,165
• Joined: 27-December 08

Posted 15 December 2010 - 06:37 PM

pbl's solution is definitely more efficient in this regard; however, Dogstopper's solution is more organized and modular in my opinion.

### #8 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

Posted 15 December 2010 - 06:42 PM

macosxnerd101, on 15 December 2010 - 07:37 PM, said:

pbl's solution is definitely more efficient in this regard; however, Dogstopper's solution is more organized and modular in my opinion.

And that's what I'm saying. In the PRACTICAL sense, his is more efficient. But mine uses organized higher order data structures.

### #9 skyerz

Reputation: 4
• Posts: 211
• Joined: 14-October 10

Posted 17 December 2010 - 04:00 AM

i have a little knowledge on HashMaps however i wanted to learn both ways, this is what i done so far, im not sure if what i have started is right however how would i be able to read from the words file and assign it to the specific number from this point
```import java.io.*;
import java.util.*;

public class Words {
public static void main (String [] args)throws IOexception
{
//int [] list = new int [9];
for(int i=0;i <9;i++)
{
HashMap<String, letters> alphabet = new Hashmap<String, letters>();

while(true)
{
if (line ==null)
break;
String
}
}

}

}

```

### #10 Dogstopper

Reputation: 2950
• Posts: 11,217
• Joined: 15-July 08

Posted 17 December 2010 - 05:44 AM

Here's teh basics on Maps:

Basically, you have one or more values that map to one "key", meaning when I get the key called 3, you have "d", "e", and "f" associated with it.

### #11 g00se

• D.I.C Lover

Reputation: 3105
• Posts: 13,388
• Joined: 20-September 08

Posted 17 December 2010 - 07:44 AM

Don't forget that the backing data for 'bat' and 'cat' will be identical. You probably want: SortedMap<String, List<String>> or even SortedMap<String, SortedSet<String>>

This post has been edited by g00se: 17 December 2010 - 07:45 AM

### #12 skyerz

Reputation: 4
• Posts: 211
• Joined: 14-October 10