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.


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

Results 1 to 2 of 2

Thread: Strings

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

    Thumbs up Strings

    Hello, obviously you can see that I am new to Java and programming. I was assigned this project which is now turned in and graded. But I would like to know how to do it properly, it has to do as you might have guessed Strings, so what the question was asking was to do this
    Create a class with the following 2 static methods. Also create a test class to verify that your methods are working correctly. You may use the examples below as test cases, but add a few new ones too. You will want to use the StringBuilder class for these methods.

    1. Given a string, compute a new string where identical chars that are adjacent in the original string are separated from each other by a "*".

    Examples are as shown below::
    function name is public static String addStars(String str)
    addStars("hello") → "hel*lo"
    addStars("xxyy") → "x*xy*y"
    addStars("aaaa") → "a*a*a*a"

    2. Given a string, compute a new string where all the lowercase 'x' chars have been moved to the end of the string.

    Examples are as shown below::
    function name is public static String moveXs(String str)

    moveXs("xxre") → "rexx"
    moveXs("xxhixx") → "hixxxx"
    moveXs("xhixhix") → "hihixxx"

    And I had a Code that just failed straight across the board(monitor) and I was wondering how could I do this? Well I came up with this pseudo code.

    Well what I knew was that any re-occurrences of a letter right after a letter would result in a putting of a "*" after the first occurrence. So I was wondering how I would do that.
    The second part of it would be to take all x's from a string and put them at the end I was also wondering how to do that.

    My teacher said it had to do with string buffers but I have not a clue how to use string buffers. Please if you could just help me understand how to do this.

  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    My Mood
    Thanked 93 Times in 86 Posts

    Default Re: Strings

    OK, here we go.
    For your first method you can loop over the original String and compare the character at the current index with the character at current +1. Add * if they are identical.
    The second one could be either done with shifting every x found, or you just count all 'x's, remove them from the String and append the number of 'x's found at the end.
    Start simple, one step after the other. first loop over the String. Next try to find identical adjacent chars. After that try to insert * at the right spot. Use loads of System.out.println() statements, to see what your methods do.

Similar Threads

  1. Question regarding Strings
    By yeeesh in forum Java Theory & Questions
    Replies: 2
    Last Post: November 30th, 2010, 12:09 PM
  2. I need help! Re: strings, nextLine, while and more..
    By rockerade in forum What's Wrong With My Code?
    Replies: 19
    Last Post: October 5th, 2010, 03:51 PM
  3. Strings
    By Leeds_Champion in forum Algorithms & Recursion
    Replies: 3
    Last Post: November 3rd, 2009, 10:09 PM
  4. Strings
    By BeSwift21 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2009, 07:02 PM
  5. Replies: 2
    Last Post: June 19th, 2008, 03:58 AM

Tags for this Thread