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 4 of 4

Thread: java replace correct number into letter

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

    Default java replace correct number into letter

    Well, this is my first time get here.

    I'm trying to figure out the correct way to replace number into letter. In this case, I need two steps.

    First, convert letter to number. Second, restore number to word.

    Words list: a = 1, b = 2, f = 6 and k = 11.

    I have word: "baafk"

    So, for first step, it must be: "211611"

    Number "211611" must be converted to "baafk".

    But, I failed at second step.

    Code I've tried:
    public class str_number {
    public static void main(String[] args){
        String word = "baafk";
        String number = word.replace("a", "1").replace("b","2").replace("f","6").replace("k","11");
        System.out.println(word);
        System.out.println(number);
     
        System.out.println();
     
        String text = number.replace("11", "k").replace("6","f").replace("2","b").replace("1","a");
        System.out.println(number);
        System.out.println(text);
        }
    }
    Result for converting to number: baafk = 211611

    But, result for converting above number to letter: 211611 = bkfk

    What do I miss here?

    How to distinguish if 11 is for "aa" and for "k"? Do you have any solutions or other ways for this case?

    Thank you.


  2. #2
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: java replace correct number into letter

    The most direct way to address this would be to add a delimiter between each number so that you can tell the difference between "11" for a "k", and "1,1" (using a comma delimiter) for two "a"s.

    What is the background behind your question? I.e., why do you need to do this conversion?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,374
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: java replace correct number into letter

    How to distinguish if 11 is for "aa" and for "k"?
    I don't know that there is a way. The numbers need delimiters/boundaries so you can tell where they begin and end.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: java replace correct number into letter

    Another way would be to use 2 digits to represent each letter, e.g., a = 01, b = 02, etc. This way you don't need to use delimiters/boundaries.

Similar Threads

  1. JAVA = Replace a line in a text file
    By JGW in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 16th, 2013, 04:46 AM
  2. [SOLVED] Hangman Program Java: Comparing the User's Guess with Another String Letter by Letter
    By theonlydvr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 29th, 2013, 05:35 PM
  3. Print a letter of the alphabet based on a number that is input
    By justlearning in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 20th, 2013, 01:36 PM
  4. Count Number of Each Letter in Given Word?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 11th, 2011, 07:55 PM
  5. letter to number
    By silverspoon34 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 27th, 2009, 06:01 AM