Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0

    Need help replacing characters in a word with certain letters


    Hello, I am writing a code that takes a string inputted by the user and codes it. There are 4 steps to it and I have figured out how to do the last 3 steps. Here are the 4 steps:

    Step 1: (this is the step I need help on)
    Replaces each character in the entered word using the following rules:
    A, E, I, O, U, Y → A
    B, F, P, V → B
    C, G, K, J, Q, S, X, Z → C
    D, T → D
    H, W → H
    L → L
    M, N → M
    R → R

    Step 2:
    For any consonant that is followed by an H, eliminate the H. Example, a string like DHD
    would be replaced by DD, but AHD would remain the same.

    Step 3:
    Replace any repeated sequence of consonants by a single instance. For example, MM would be
    replaced by M.

    Step 4:
    Eliminate all the As.


    So basically, I was wondering if someone can show me an example of seeing if the letters B, F, P, V are in the string and if they are replace them with a B. I would be able to figure it out from there, thank you.
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    are you allowed to use regular expressions?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by bullet
    are you allowed to use regular expressions?
    yes, I believe so
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by vex91
    yes, I believe so
    well regular expression would work for this.

    but for this particular problem, it may be simpler for you to use a loop and switch
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Some of the rules don't make sense where a letter is replaced with itself:
    R -> R or A -> A

    Removing those rules would make the code shorter.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Ok so I thought I figured it out but step 1 is still giving me some trouble. I have tested step one in its own program and it works fine but when I put it into the rest of the code it does not do what it is suppose to.

    If I input the word "thinning" I should be getting this output:
    After Rule 1: DHAMMAMC
    After Rule 2: DAMMAMC
    After Rule 3: DAMAMC
    Result: DMMC
    Would you like to perform another translation? y/n

    But instead I am getting this output:
    After Rule 1: THINNING
    After Rule 2: TINNING
    After Rule 3: TINING
    Result: TINING
    Would you like to perform another translation? y/n

    Here is my code:

    --------------------------------------------------------------------------------------------------------------------------------------------

    Code:
    import java.util.Scanner;
    public class PlanitReduction {
    
    /**
    * Checks if the char identified by "currentChar" is a consonant.
    * @param currentChar the char we are testing. Assumed to be UPPERCASE.
    * @return true if currentChar is a consonant, false otherwise.
    */
    
    private static boolean isConsonant(char currentChar){
    
    if((currentChar == 'A') ||
    (currentChar == 'E') ||
    (currentChar == 'I') ||
    (currentChar == 'O') ||
    (currentChar == 'U')){
    return false;
    }
    
    return true;
    }
    
    public static void main(String[] args) {
    String answer = "y";
    Scanner scan = new Scanner(System.in);
    
    do{
    String original = "";
    String encoded = "";
    
    //Getting the original message
    System.out.println("Please enter your original word: ");
    original = scan.next();
    
    //Getting the whole word in upper case
    original = original.toUpperCase();
    
    //Step 1: Replace each character with it's appropriate letter: 
    
    String coded1 = original.replace('a', 'a')
    .replace('e', 'a')
    .replace('i', 'a')
    .replace('o', 'a')
    .replace('u', 'a')
    .replace('y', 'a');
    String coded2 = coded1.replace('b', 'b')
    .replace('f', 'b')
    .replace('p', 'b')
    .replace('v', 'b');
    String coded3 = coded2.replace('c', 'c')
    .replace('g', 'c')
    .replace('k', 'c')
    .replace('j', 'c')
    .replace('q', 'c')
    .replace('s', 'c')
    .replace('x', 'c')
    .replace('z', 'c');
    String coded4 = coded3.replace('d', 'd')
    .replace('t', 'd');
    String coded5 = coded4.replace('h', 'h')
    .replace('w', 'h');
    String coded6 = coded5.replace('l', 'l');
    String coded7 = coded6.replace('m', 'm')
    .replace('n', 'm');
    encoded = coded7.replace('r', 'r');
    
    
    System.out.println("After Rule 1: " + encoded);
    
    //Step 2: Removes all H's if their occur after a consonant
    
    for(int i = 1; i < encoded.length(); i++){
    
    char currentChar = encoded.charAt(i);
    
    if(currentChar == 'H'){
    
    char previousChar = encoded.charAt(i-1);
    
    if(isConsonant(previousChar)){
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    }
    }
    }
    
    System.out.println("After Rule 2: " + encoded);
    
    
    //Step 3: Getting rid of repeated consecutive consonants
    
    char previousChar = encoded.charAt(0);
    for(int i = 1; i < encoded.length(); i++)
    {
    char currentChar = encoded.charAt(i);
    
    if(isConsonant(currentChar) && (currentChar == previousChar))
    {
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    i--;
    }
    previousChar = currentChar;
    }
    
    System.out.println("After Rule 3: " + encoded);
    
    // Step 4: Eliminating all A's
    
    for(int i = 0; i < encoded.length(); i++)
    {
    char currentChar = encoded.charAt(i);
    
    if(currentChar == 'A'){
    
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    i--;
    }
    }
    System.out.println("Result: " + encoded);
    System.out.println("Would you like to perform another translation? y/n");
    answer = scan.next();
    
    }while(!answer.equals("n"));
    
    System.out.println("Thank you!");
    scan.close();
    }
    
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What case is the String be checked for lowercase letters?
    Why do this:
    Code:
    .replace('a', 'a')
    Please wrap your code in code tags to make it easier to read.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    What case is the String be checked for lowercase letters?
    Why do this:
    Code:
    .replace('a', 'a')
    Please wrap your code in code tags to make it easier to read.
    It is taking the lowercase entered string and changing it to upper case. Is that what you mean?
    And yeah I was going to edit out the
    Code:
    .replace('a', 'a')
    when the code was finished
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    What case are the characters in the String: original when it is checked for lowercase letters with the replace statement?


    And yeah I was going to edit out the
    Sounds like you are writing code before you are thinking about what the code should do.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    What case are the characters in the String: original when it is checked for lowercase letters with the replace statement?
    Lower case, and it is being changed to upper case
  20. #11
  21. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Will that work like you want?
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Will that work like you want?
    It seems to be working, its changing the lower case letters to upper case. But thats not my problem, I can't figure out why when I enter "thinning" why it just outputs "THINNING" instead of replacing the characters like it is suppose to. For some reason it is making the String 'encoded' just equal to the entered word but in upper case, instead of actually replacing the words.
  24. #13
  25. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    why when I enter "thinning" why it just outputs "THINNING"
    Look at this line of code:
    Code:
    original = original.toUpperCase();
    and think about what you just said.
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by NormR
    Look at this line of code:
    Code:
    original = original.toUpperCase();
    and think about what you just said.
    No you are not understanding me, I know its in upper case. Again, it is NOT the upper/lower case that I am having problems with. The problem is that when I enter "thinning" I am getting "THINNING" instead of "DHAMMAMC". The program isn't doing step 1 for some reason.

    Here is my code again:
    Code:
    import java.util.Scanner;
    public class PlanitReduction {
    
    /**
    * Checks if the char identified by "currentChar" is a consonant.
    * @param currentChar the char we are testing. Assumed to be UPPERCASE.
    * @return true if currentChar is a consonant, false otherwise.
    */
    
    private static boolean isConsonant(char currentChar){
    
    if((currentChar == 'A') ||
    (currentChar == 'E') ||
    (currentChar == 'I') ||
    (currentChar == 'O') ||
    (currentChar == 'U')){
    return false;
    }
    
    return true;
    }
    
    public static void main(String[] args) {
    String answer = "y";
    Scanner scan = new Scanner(System.in);
    
    do{
    String original = "";
    String encoded = "";
    
    //Getting the original message
    System.out.println("Please enter your original word: ");
    original = scan.next();
    
    //Getting the whole word in upper case
    original = original.toUpperCase();
    
    //Step 1: Replace each character with it's appropriate letter: 
    
    String coded1 = original.replace('a', 'a')
    .replace('e', 'a')
    .replace('i', 'a')
    .replace('o', 'a')
    .replace('u', 'a')
    .replace('y', 'a');
    String coded2 = coded1.replace('b', 'b')
    .replace('f', 'b')
    .replace('p', 'b')
    .replace('v', 'b');
    String coded3 = coded2.replace('c', 'c')
    .replace('g', 'c')
    .replace('k', 'c')
    .replace('j', 'c')
    .replace('q', 'c')
    .replace('s', 'c')
    .replace('x', 'c')
    .replace('z', 'c');
    String coded4 = coded3.replace('d', 'd')
    .replace('t', 'd');
    String coded5 = coded4.replace('h', 'h')
    .replace('w', 'h');
    String coded6 = coded5.replace('l', 'l');
    String coded7 = coded6.replace('m', 'm')
    .replace('n', 'm');
    encoded = coded7.replace('r', 'r');
    
    
    System.out.println("After Rule 1: " + encoded);
    
    //Step 2: Removes all H's if their occur after a consonant
    
    for(int i = 1; i < encoded.length(); i++){
    
    char currentChar = encoded.charAt(i);
    
    if(currentChar == 'H'){
    
    char previousChar = encoded.charAt(i-1);
    
    if(isConsonant(previousChar)){
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    }
    }
    }
    
    System.out.println("After Rule 2: " + encoded);
    
    
    //Step 3: Getting rid of repeated consecutive consonants
    
    char previousChar = encoded.charAt(0);
    for(int i = 1; i < encoded.length(); i++)
    {
    char currentChar = encoded.charAt(i);
    
    if(isConsonant(currentChar) && (currentChar == previousChar))
    {
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    i--;
    }
    previousChar = currentChar;
    }
    
    System.out.println("After Rule 3: " + encoded);
    
    // Step 4: Eliminating all A's
    
    for(int i = 0; i < encoded.length(); i++)
    {
    char currentChar = encoded.charAt(i);
    
    if(currentChar == 'A'){
    
    StringBuilder sb = new StringBuilder(encoded);
    sb.deleteCharAt(i);
    encoded = sb.toString();
    i--;
    }
    }
    System.out.println("Result: " + encoded);
    System.out.println("Would you like to perform another translation? y/n");
    answer = scan.next();
    
    }while(!answer.equals("n"));
    
    System.out.println("Thank you!");
    scan.close();
    }
    
    }
  28. #15
  29. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    when I enter "thinning" I am getting "THINNING"
    Do you understand that THINNING is the uppercase of thinning?

    Print out the value of the variable: original before and after this statement is executed:
    Code:
    original = original.toUpperCase();

    Try using the toLowerCase() method and see what happens.


    Where are there any uppercase characters in 's in the replace() method calls?
    Last edited by NormR; October 10th, 2013 at 09:21 PM.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo