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

Thread: I need help coding a string

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need help coding a string

    Hello, I am currently writing a program that takes a word inputted by the user and codes it. It has 4 steps and I have figured out and have finished the last 3 steps but can't quite figure out the first one. Here are the 3 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 A’s.

    -----------------------------------------------------------------------------------------
    And here is my code, I have 90% of it finished.

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

    }

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

    Basically, I was wondering if someone can show me the code of how to, if the letters A, E, I, O, U, Y are included in the entered string then replace them with the letter A. That would be so helpful. Thank you


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I need help coding a string

    Improving the world one idiot at a time!

  3. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: I need help coding a string

    I don't really know how to do it with StringBuilder but with the String class, you can replace chars with new ones with this:

    String (Java Platform SE 7 )

    for(int i = 1; i < encoded.length(); i++){

    That is going to ignore the first character as String indexes start at 0.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help coding a string

    Sorry, I didnt think those were the same forums. Anyone able to help?

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: I need help coding a string

    @ vex91

    No they are not the same forums but many people read more than one, just like you did. The problem with cross posting is that I began giving you help in your other thread. If someone posted a reply here without realising the other thread existed they would be wasting their time.

    @Goodbyeworld

    I suggested using a StringBuilder in the other thread to build up a new String char by char if the String methods were off limits.
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need help coding a string

    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:

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

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

    }

Similar Threads

  1. Need help with some coding
    By Firearrow5235 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: April 13th, 2013, 06:57 AM
  2. Need Help With Coding
    By noles227 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2012, 07:35 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM
  5. Need your help to coding
    By Tony_nguyen19 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 14th, 2011, 08:58 AM