Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need help with this algorithm

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with this algorithm

    I need a Java algorithm that converts a string of numbers into text. It is related to how a phone keypad works where pressing 2 three times creates the letter "c" or pressing 4 one time creates the letter "g". For example a string of numbers "44335557075557777" should decode to "help pls" where 0 equates to a space.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,985
    Thanks
    52
    Thanked 2,271 Times in 2,243 Posts

    Default Re: Need help with this algorithm

    Can you make a table of keypresses to character?
    So far you've shown:
    222 > 'c'
    4 > 'g'
    0 > ' '

    Can you show how "44335557075557777" decodes to "help pls"?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Need help with this algorithm

    Can you show how "44335557075557777" decodes to "help pls"?
    I think it was decoded the way how the cellphone writes a text (not the querty phone)

    this is the table:
    Key Num: values:
    1 I thinks it has special characters
    2 abc
    3 def
    4 ghi
    5 jkl
    6 mno
    7 pqrs
    8 tuv
    9 wxyz
    0 <space>

    44 > h
    33 > e
    555 > l
    7 > p
    0 > <space>
    7 > p
    555 > l
    7777 > s

  4. #4
    Member
    Join Date
    Feb 2014
    Location
    India
    Posts
    47
    My Mood
    Bored
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default Re: Need help with this algorithm

    Firstly you can extract out the consecutive occurence of numeric entry in your text and then you may have a hashmap in which for each combination of numeric values, you can have a corresponding alphabet. This means you will have 26 values in the hashmap corresponding to each alphabet, then a blank space for 0 and "/" for 1.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Need help with this algorithm

    you can also try using hashmap with Integer(0-9) as key and String as value and then store the 10 key value pair as you have specified. also have two arrays key[] and numOfTaps[]. so when you press something like "44335557075557777" , extract each key pressed in key[] array and the num of times it is pressed in numOfTaps[].
    for the above example
    key[]={4,3,5,7,0,7,5,7}
    and numOfTaps={2,2,3,1,1,1,3,4}
    then the String (say str) associated with key "key[i]" in your hashmap. (key[0] is 4 so it will have "ghi" as value)
    and then get the character as str[numOfTaps[i]] (so u will get "h" for i=0)

Similar Threads

  1. Replies: 16
    Last Post: November 21st, 2013, 07:49 AM
  2. Replies: 3
    Last Post: June 10th, 2013, 04:42 AM
  3. all i need is algorithm
    By coder.freak in forum Paid Java Projects
    Replies: 3
    Last Post: April 6th, 2011, 11:11 AM
  4. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM