Results 1 to 6 of 6
  1. #1
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Need help replacing characters in a word with certain letters

    Hello, I am trying to write a program that codes an entered string. The program has 4 steps and I need some help. I am stuck on step 1, I have figured out the other 3 steps and have 90% of the code already finished, but I can't figure out how to do step 1. The 4 steps are:

    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 A’s.



    I have attached my code in a .txt file to this post so you can take a look at it. Please, any help will be appreciated I've been stuck on this part for forever. Thank you.
    Attached Files Attached Files
    Last edited by vex91; 10-10-2013 at 05:35 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need help replacing characters in a word with certain letters

    Post your code. Do not make people follow links or download files.

    The String class has methods for replacing chars. But remember that Strings are immutable and those methods return new Strings which you need to assign to a variable. If you cannot use the String methods and must code it yourself then iterate over the original String one char at a time and add the converted char to a StringBuilder. At the end return a String representation of the StringBuilder.

  3. #3
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Need help replacing characters in a word with certain letters

    Not gonna lie, I didn't understand what you just said. Could you please give me an example? Would it be possible to show me the code necessary to turn the letters B, F, P, V into the letter B? If you could that would be so helpful and I could probably figure out the rest from here. Oh and here is my 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 uppercase
    original = original.toUpperCase();

    //Step 1: Replace each character with it's appropriate letter:

    //************************************************** ***********
    //
    // ? ? ? ? ? ? ?
    //
    //************************************************** ***********


    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();
    }

    }

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need help replacing characters in a word with certain letters

    Java Code:
    String text = "ABCDE";
    if(text.charAt(4) == 'A' || text.charAt(4) == 'E') {
        System.out.println('A');
    }
    There's a snippet to get you started.

  5. #5
    vex91 is offline Member
    Join Date
    Sep 2013
    Posts
    12
    Rep Power
    0

    Default Re: Need help replacing characters in a word with certain letters

    Thanks, I am starting to figure out what to do. But in your snippet of code, it is only showing a way to do it if the string is part of the code. How would I do this if I am taking a string inputted by the user? Because I wouldn't be able to use the charAt since I wouldn't know the exact number of characters in the string the user has entered and it would most likely be different every time.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Need help replacing characters in a word with certain letters

    Are you serious?

    Java Code:
    String text = "ABCDE";  // change this to
    String text = input from user

Similar Threads

  1. Replies: 2
    Last Post: 05-30-2012, 12:31 AM
  2. Replies: 2
    Last Post: 11-22-2011, 09:22 PM
  3. Replacing characters at a certain index
    By Slayer in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 09:49 PM
  4. Replacing Vowels in a word.
    By mklprasad in forum Advanced Java
    Replies: 1
    Last Post: 10-05-2009, 12:31 PM
  5. replacing characters???
    By manda147 in forum New To Java
    Replies: 2
    Last Post: 11-29-2008, 08:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •