Saturday, September 13, 2008

Digit to Two Letter Word Converter Utility

/*
* A utility for displaying combinations of letter groups, two at a time.
* The user will enter digits (0-9) on a cell-phone numeric pad, letter groups
* for each digits(eg. "ABC", "WXYX" etc are stored in a map using the letter
* key for each. Keys entered at command line by user, are mapped to their
* corresponding values("letter groups") and are retrieved and saved into
* an String array.
*
* The string arry then is passed along with other info to make the combinations.
*
* There is no definate requirement for this utility, so it servs to give a general
* idea of how user input are mapped to values and how they are combined into meaningful
* two-letter words.
*
* Please feel free to call if you need to.
*
* Assumptions: 1.Combination of two letters are to be displayed
* 2. The order of letter-group to be used for first letter doesn't matter.
* eg. if "ABC" "DEF" are entered this utility could either print:
* AD, AE, AF, BD, BE, BF, CD, CE, CF
* or
* DA, DB, DC, EA, EB, EC, FA, FB, FC
*
*
* Author Date Category
* Nirmal Singh Sep. 13th 2008 General/Utilities
*
*
* note: I can produce a more refined version, depending on input/comments in case
* somebody needs this utility for ceartain use.
*
*
*/

package mine_code;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class TwoLetterCombination
{
public static void main(String[] args)
{

getCommandLineArgsProcess(args);
}

private static void getCommandLineArgsProcess(String[] privateArgs)
{
System.out.println("Please enter digits 0-9 and ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = null;

try
{
s = in.readLine();
String[] strKeys = new String[s.length()];
int[] intKeys = new int[s.length()];

for(int b=0;b {
strKeys[b] = String.valueOf(s.charAt(b));
intKeys[b] = Integer.valueOf(strKeys[b]);
}

callToCopy(strKeys,intKeys, s);
}

catch (IOException e)
{
e.printStackTrace();
}
//System.out.println("Digits entered are: " + s);
}

public static void callToCopy(String[] strArr, int[] intArr, String s)
{
String[] targetArr = new String[strArr.length];
Map myHash = new HashMap();
for(int g=0;g {
targetArr[g] = strArr[g];

switch (intArr[g])
{
case 0: myHash.put(0, " ");break;
case 1: myHash.put(1, " "); break;
case 2: myHash.put(2, "ABC"); break;
case 3: myHash.put(3, "DEF"); break;
case 4: myHash.put(4, "GHI"); break;
case 5: myHash.put(5, "JKL"); break;
case 6: myHash.put(6, "MNO"); break;
case 7: myHash.put(7, "PQRS"); break;
case 8: myHash.put(8, "TUV"); break;
case 9: myHash.put(9, "WXYZ"); break;
}
}
printCombinations(myHash, s);
}

public static void printCombinations(Map myHash, String s)
{
Iterator it = (myHash.keySet()).iterator();
String[] values = new String[myHash.size()];
int arrSize=0;
System.out.println("Digits entered are: " + s);
System.out.println("The following are digits entered and their corresponding letter"+
"groups: ");
System.out.println();

while(it.hasNext())
{
int key = (Integer)it.next();
values[arrSize] = myHash.get(key).toString();
System.out.println(key + " " + values[arrSize]);
arrSize++;
}

int sizeOfArr =arrSize;
System.out.println();
System.out.println("The following are combinations of the letters: ");
System.out.println("Three blank spaces are stored for each 1 and 0 digits "+'\n'+
"and I am displaying the spaces along with the corresponding "+'\n'+
"letters as part of combination. ");
System.out.println();
for(int m=0; m {
for( int k=0;k {
if(m+1 {
for( int j=0;j {
String tempStr;
char firstChar, secondChar;
firstChar = values[m].charAt(k);
secondChar = values[m+1].charAt(j);
tempStr = firstChar+""+secondChar+"";
System.out.println(tempStr);
}
}
}
}
}
}

0 comments: