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: Finding a word within a string (using a stringbuilder)

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Finding a word within a string (using a stringbuilder)

    How would I go about checking to see if, for example, the word 'happy' can be made out of a string of letters, e.g. 'happybirthday'. In my program they are both sorted first, though, so technically I'm actually looking for 'ahppy' in 'aadbihhpprtyy'... How can I do this using a stringbuilder?

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Finding a word within a string (using a stringbuilder)

    Not sure I understand what you are asking. If characters have been inserted into a StringBuilder object, there are methods like charAt() that can be used to look at individual characters in the SB object.

    Can you explain in more detail? Where do the letters in the StringBuilder object come from?
    Where do the Strings with the letters to search for come from?
    Is it a search for matches? If the word has some letters, is the idea to search the SB object to see if it has those letters?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Finding a word within a string (using a stringbuilder)

    I have an EntryIterator that iterates over every word in the dictionary. They are alphabetized after being sorted, e.g. happy is ahppy.

    User inputs jumbled letters and an integer number. The program cycles through the dictionary (using the iterator) and looks for words of the given length (e.g. 5) that can be made with the jumbled letters.

    So, if the user enters 'aadbihhpprtyy' and '5', the program should iterate through the dictionary and ultimately return ahppy because 1) its length is 5, and 2) all of its letters exist within the user's jumbled input. Satisfying condition #2 is what I'm not sure how to do.

    The instructions given for this part of the project are:
    For each letter in the user's jumbled input, if it matches the current position in the dictionary word with [user's input # of] letters, increment the current position. Otherwise, add that letter to a stringbuilder. If all of the letters in the dictionary word (e.g. ahppy) were found in the jumble ('aadbihhpprtyy'), then create a new string from the stringbuilder (the leftover letters). See if that new string can be used to make a second word. In this case, the leftover letters would create "abdhirty" which can be unscrambled to birthday. Ultimately, we find that our original jumble can be unscrambled to the phrase "happy birthday."

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Finding a word within a string (using a stringbuilder)

    Are you having problems using any of the java classes' methods?

    Otherwise it looks like a program design problem that involves searching through some arrays.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: March 28th, 2013, 09:54 AM
  2. [SOLVED] String Matcher finding only char not a whole string
    By Kakashi in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 18th, 2011, 09:58 AM
  3. Replies: 2
    Last Post: February 25th, 2010, 04:17 PM
  4. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM