Convert letters into numbers

I need to write a method that converts letters from a word into a number according to the telephone buttons.
I figured I was at a good start by putting the string of the word into a character array, but how would I assign each of those letters to its corresponding number?
Thanks in advance.

changes to code that fills the hashmap. more concise (less interpretable)
import java.util.HashMap;
import java.util.Map;
class HashMapTester2
    private Map<Character, Integer> phoneHash;
    // constructor
    // for converting alphabetical characters to numbers based on the
    // phone dialing pad.
    public HashMapTester2()
        phoneHash = new HashMap<Character, Integer>();
        fillMap();
    private void fillMap()
        for (char myChar = 'a'; myChar <= 'z'; myChar++)
            int distanceFrom_a = (int)(myChar - 'a');
            if (myChar > 'r') // 's' hangs back w/ the pqr group.
                distanceFrom_a -= 1;
            if (myChar > 'y')  // 'z' hangs back w/ the wyz group.
                distanceFrom_a -= 1;
            // 2 is due to the numbers starting at 2
            // 3 is due to the letters being grouped in 3's, all sharing
            // the same number.
            int number = (int)(2 + (distanceFrom_a) / 3);
            phoneHash.put(myChar, number);  // insert reg char
            // insert uppercase version, same number
            phoneHash.put(Character.toUpperCase(myChar), number);
    // convert string to a long number based on results of
    // the string's characters run through the hashmap
    public long convertStr(String s) throws IndexOutOfBoundsException
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++)
            char myChar = s.charAt(i);
            if ((myChar >= 'a' && myChar <= 'z') || (myChar >= 'A' && myChar <= 'Z'))
                sb.append(phoneHash.get(myChar));   
            else
                throw new IndexOutOfBoundsException();
        return Long.valueOf(sb.toString());
    public static void main(String[] args)
        HashMapTester2 hmt = new HashMapTester2();
        System.out.println(hmt.convertStr("aaa"));
        System.out.println(hmt.convertStr("What"));
        System.out.println(hmt.convertStr("the"));
        System.out.println(hmt.convertStr("heck"));
        System.out.println(hmt.convertStr("Fubar"));
        System.out.println(hmt.convertStr("Snafu"));
        System.out.println(hmt.convertStr("abcdefghijklm"));
        System.out.println(hmt.convertStr("nopqrstuvwxyz"));
        System.out.println();
        System.out.println(hmt.convertStr("abc"));
        System.out.println(hmt.convertStr("def"));
        System.out.println(hmt.convertStr("ghi"));
        System.out.println(hmt.convertStr("jkl"));
        System.out.println(hmt.convertStr("mno"));
        System.out.println(hmt.convertStr("pqrs"));
        System.out.println(hmt.convertStr("tuv"));
        System.out.println(hmt.convertStr("wxyz"));
}

Similar Messages

  • How to convert pages into numbers

    How to convert documents in
    pages into numbers for processing

    I responded to you here > How to convert pages document into...: Apple Support Communities
    Please do not start duplicate topics 

  • Can one convert .pdf into .numbers?

    I want to convert .pdf files into .numbers. I've heard it is not possible, but I'd like to confirm it

    Thanks for your answer.
    Yes, that seems to be right. However, I now believe that what one can do is convert .pdf into .xls which Numbers, more or less well, can read, and then save the file as .numbers.

  • HOW TO CONVERT DATE INTO NUMBERS

    HI,
    what the syntax for converting date into No. Ex from the first date of the month till current date how many days are there, this No of days needs to be divided by the net sale of the month.
    i have searched in formulas fields but i am not able to find such option...
    please help.
    Thanking you,
    UMAR NAYAB.

    Hi,
    Stratos and Sastry
    the formula given by you shows the variation for the current month of suppose 30 days
    but if the month is of 31 days or 28 days (feb) how will the system will able to generate the report accordingly.
    i want to make a report in which it should show the net sales by the current month and current date.
    so the system should automated by the number of days in a month.
    thanking you..
    UMAR NAYAB.

  • How to converts string into numbers

    As a beginer now i am asking the above question?
    now i am working with XCode 4.3

    I responded to you here > How to convert pages document into...: Apple Support Communities
    Please do not start duplicate topics 

  • Hello there! i am using Adobe acrobat pro DC and using windows 8.1. Recently I was trying to convert a web page (includes greek letters) into pdf. I noticed that the conversion is not accurate, some words are missing, some are misplaced. Especially greek

    hello there! i am using Adobe acrobat pro DC and using windows 8.1. Recently I was trying to convert a web page (includes Greek letters) into PDF. I noticed that the conversion is not accurate, some words are missing, some are misplaced. Especially Greek words are not accurately converted or even not converted. Please give me some suggestions to improve this issue. Regards

    Hey quantum info,
    Thank you for posting at Adobe forums. We would love to help you out.
    Could you please let me know if you have tried converting other web pages to PDF and checked if the same issue persists.
    How have you been trying to convert the web page to PDF?
    Are you trying to convert to PDF directly via browsers or using "File > Create > PDF from Web Page"  option in Acrobat.
    Let me know more so that I can analyze where actually the issue persists.
    Hope to hear from you.
    Regards,
    Anubha

  • Each time I type an apostrophe on pages it comes out as a divide sign and alters any preceding italicised text into meaningless letters and numbers. How do I change this?

    Each time I type an apostrophe on pages it comes out as a divide sign and alters any preceding italicised text into meaningless letters and numbers. How do I change this?

    Does this still happen if you set your font to Lucida Grande?
    Could you use the camera icon here to post a screen shot of how this looks with the meaningless letters and numbers?

  • How do I convert an exported excel document to numbers? I am able to get the data into numbers but I can't read it.

    I am trying to get an excel document I exported from Wufoo into numbers on my mac. It works but I cannot read the data.

    I got it to work!! Just had to select Commas (.csv) instead of excel for the export. What happened before was that I chose .xls to export the data and when I tried to open the file in Numbers, it showed an error.
    Import Warning - This is a tab delimited document, not a valid Excel document. The data might look different.
    the data was all symbols etc..
    Thanks for replying to my question!

  • Trying to install Adobe Photodeluxe (elements 13)   I have box and cd sleeve with serial number,   But registration won't let me type letters only numbers allowed.  Help please.

    Trying to install Adobe Photodeluxe (elements 13)   I have box and cd sleeve with serial number,   But registration won't let me type letters only numbers allowed.  Help please.

    You possibly have a redemption code which has to be converted into a serial number.
    https://helpx.adobe.com/x-productkb/global/find-serial-number.html

  • How to update small letters into a database table

    Hi Guys,
                    Iam updating a database table. I want to update lower case letters into the table. But by default all the lower case gets converted to Uppercase. How to acheive this requriement.?
    Regards.
    Harish.

    hi
    On the domain of the particular field , within the definition tab you have to select the lower case check box . Only if this is checked then alone the values with lower case will be saved else it will be converted to upper case by default.
    Hope this will answer your need.
    Please give 10 points
    - Dheepak

  • Import a Zip Code Field Into Numbers

    I'm sure this has been asked before, but I've done a couple of searches and can't find the answer.
    I often need to import a list of addresses into a spreadsheet for cleaning up. In Excel, when I do this, I "open" a text file and get the "import wizard." I can then format the zip code field as a text field and retain any leading zeros (e.g., 00678 is not converted to the number 678).
    How can I do this in Numbers?
    Message was edited by: Joel Mcintosh1

    Blazeland wrote:
    What if the data is of varying lengths? I data with 10, 12 or 13 digit numbers that sometimes have leading zeros. I can't open csv files into Numbers because it deletes the zeros and there is not standard length to the data.
    It's certainly not the reason explaining that you can't open the Numbers generated csv files.
    Numbers put a delimiter between values so their length is meaningless.
    The problem may be the value of the delimiter.
    As I wrote MANY times, if the decimal separator used by the running operating system is period, Numbers use comma as values delimiter.
    If the decimal separator is comma, Numbers use the semi-colon as values separator.
    Yvan KOENIG (from FRANCE mardi 24 mars 2009 21:13:18)

  • My serial number will not work because it is letters and numbers...

    My serial number will not work because it is letters and numbers but I can't type letters into the boxes on the Adobe installer.
    I've tried holding shift, putting caps lock on, and leaving it lower case.  It just won't let me type letters into the boxes.  Is there a work around or some other solution?

    Redemption Code Help
    Mylenium

  • Keyboard letters and numbers not working after (?) serious memory use (?)

    So I've been having problems with my keyboard on my MBP 15-inch, Early 2011 (OS version 10.7.5, fully updated). It stops working after I use Photoshop or InDesign (cs6) for a long period of time or when I'm surfing the web a lot. The best example where it for sure stops working when surfing the web is when I'm on tumblr and I scroll through dozen of pages on my feed. When it stops working, the only keys that continue to work is everything but the numerals and letters.
    I've taken it in, replaced the keyboard and talked to apple support and they can't figure it out. I assumed that it stops working because of all the loading images, info and memory that is in use when applcations are open but I don't know if it's because of memory.
    One thing that always occurs right before the keyboard stops working is this sound from the speakers that sounds like gears turning three times or three fast ticks. Only once has the letters and numbers start working and I don't now what I did to make it come back but I heard the three ticks play again.
    But my only solution to make it start again is restarting the computer.
    Does anybody have this problem/know the solution to make this stop?

    The only Upperfilters and Lowerfilters you were supposed to remove were the ones in 4D36E965 which is for the Optical Drive.
    The other keys, for example 4D36E966 and 4D36E967 etc.. are for other devices, such as the keyboard, mouse, and possibly other devices like sound and graphics. This is not good, because its hard to manually restore those filters without knowing what was in there before you deleted them.
    Did you backup the registry before deleting the keys?
    You could try a System Restore to restore the registry back to a previous state. If you boot into Safe Mode (press F8 or F5 before XP boots up), you can select a previous date to restore back to.
    Then follow these instructions http://support.microsoft.com/default.aspx?id=314060

  • Is there an easier way to convert bytes into bit(boolean) arrays?

    I am currently using this method to convert bytes into bit arrays:
    /*convert byte to int such that it is between 0-255 this is the bytes[] array
                        if ((bytes/128)==1)
                             bit[c+0]=true;
                        else
                             bit[c+0]=false;
                        if ((bytes[i]-bitInt[c+0]*128)/64==1)
                             bit[c+1]=true;
                        else
                             bit[c+1]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64)/32==1)
                             bit[c+2]=true;
                        else
                             bit[c+2]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32)/16==1)
                             bit[c+3]=true;
                        else
                             bit[c+3]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16)/8==1)
                             bit[c+4]=true;
                        else
                             bit[c+4]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8)/4==1)
                             bit[c+5]=true;
                        else
                             bit[c+5]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4)/2==1)
                             bit[c+6]=true;
                        else
                             bit[c+6]= false;
                        if ((bytes[i]-bitInt[c+0]*128-bitInt[c+1]*64-bitInt[c+2]*32-bitInt[c+3]*16-bitInt[c+4]*8-bitInt[c+5]*4-bitInt[c+6]*2)==1)
                             bit[c+7]=true;
                        else
                             bit[c+7]= false;

    You can loop through and use a bitwise operator instead. Here is an example without the loop.
    byte b = 6 ;
    if( b & Math.pow( 2, 0 ) == Math.pow( 2, 0 ) ) ;
    // the 2^0 bit is on
    if( b & Math.pow( 2, 1 ) == Math.pow( 2, 1 ) ) ;
    // the 2^1 bit is onetc...
    You should get something like 110 when you're done.
    If you're wonder what & does (no, its not a boolean &&), it takes the bits in the two numbers you give it and returns a number with all the bits on that are on for each of them.
    For example:
    10011011 &
    11001101 =
    10001001
    So if we take
    110 (6) &
    010 (2^1, or 2) =
    010 (2 again)
    Which means that the number (6) has the 2^1 bit on.

  • Converting Byte [] into float []

    Hi, I read the contents of a file into a byte array and am now trying to convert it into a float array.
    here is the code I am using
    public static float [] bytetofloat(byte [] convert){
        float [] data = new float [convert.length/2];
        for(int i = 0;i<convert.length;i+=2){
            for(int j = 0; j <data.length;j++){
            short valueAsShort = (short)( (convert[i] << 8)  | (convert[i+1] & 0xff));
            float valueAsFloat = (float)valueAsShort;
            System.out.println(valueAsFloat);
            valueAsFloat = data[j];
            System.out.println(data[j]);
        }can anyone see anythign wrong with the way I am doing this? I cant see anythign wrong but need to make sure its fine before I can continue.
    any advice on this or a better way to do it would be much appreciated.

    ultiron wrote:
    I'm pretty sure they do. The way im doing it is by taking 2 byte values and changing them to a 16 bit float.
    the way the bytes are shift they should only take up 16 bits.It's not that simple.
    First, a float in Java is always 4 bytes. The fact that it has an integer value that can fit into two bytes is irrelevant
    Second, floating point representation is not the same 2s complement that's used for byte, short, int, and long, so you're not just shifting the bits.
    For eample:
    1,  int: 00000000 00000000 00000000 00000001
    1, float: 00111111 10000000 00000000 00000000
    -1,  int: 11111111 11111111 11111111 11111111
    -1, float: 10111111 10000000 00000000 00000000Having said that, you're casting, so your step of going from short to float is correct. It doesn't just shift; it does conversions like the above. The caveats are:
    1) Is your conversion from bytes to short correct? That depends on what those bytes are supposed to be. If they're big-endian representations of 2-byte, 2s-complement numbers, then your code looks correct. You'll still have to test it of course. You'll want to focus on corner cases.
    00 00 --> 0
    00 01 --> 1
    10 00 --> 4096
    7f ff --> 32767
    80 00 --> -32768
    ff ff --> -1
    2) Can float hold all of short's values? I think it can. It obviously can't hold all of int's values, but I think it's precision is sufficient to cover short.
    By the way, is there a reason you're using float instead of double? Unless you're in an extremely memory-constrained environment, there's no reason to use double.

Maybe you are looking for