11 Replies - 4529 Views - Last Post: 06 May 2011 - 02:20 PM Rate Topic: -----

#1 skyerz  Icon User is offline

  • D.I.C Head

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

phone keypad

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
  • +

Replies To: phone keypad

#2 skyerz  Icon User is offline

  • D.I.C Head

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

Re: phone keypad

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"
Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: phone keypad

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.
Was This Post Helpful? 1
  • +
  • -

#4 pbl  Icon User is online

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: phone keypad

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
Was This Post Helpful? 1
  • +
  • -

#5 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: phone keypad

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.
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is online

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • Joined: 06-March 08

Re: phone keypad

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
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10807
  • View blog
  • Posts: 40,285
  • Joined: 27-December 08

Re: phone keypad

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.
Was This Post Helpful? 3
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: phone keypad

Posted 15 December 2010 - 06:42 PM

View Postmacosxnerd101, 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.
Was This Post Helpful? 1
  • +
  • -

#9 skyerz  Icon User is offline

  • D.I.C Head

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

Re: phone keypad

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];
		final FileReader inputFile = new FileReader("words");
		final BufferedReader inputBuffer = new BufferedReader(inputFile);
		for(int i=0;i <9;i++)
		{
			HashMap<String, letters> alphabet = new Hashmap<String, letters>();
			
			while(true)
			{
				String line1 =inputBuffer.readLine();
				if (line ==null)
					break;
				String
			}
		}
		
		
		
	}

}


Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: phone keypad

Posted 17 December 2010 - 05:44 AM

Here's teh basics on Maps:
http://download.orac...rfaces/map.html

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.
Was This Post Helpful? 1
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2831
  • View blog
  • Posts: 11,993
  • Joined: 20-September 08

Re: phone keypad

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

Was This Post Helpful? 3
  • +
  • -

#12 skyerz  Icon User is offline

  • D.I.C Head

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

Re: phone keypad

Posted 06 May 2011 - 02:20 PM

sorry to resurect this found some useful info on here, hasmaps work weill however the array is indeed better, for user input say for e.g if i wanted to type hello which is 45356, this cant be done using hash maps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1