Results 1 to 19 of 19
  1. #1
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default StringBuilder while loop problem

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:18 AM. Reason: housekeeping

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    How would you do this "by hand" without using a computer? Pretend you have a friend who has no idea how to do this- what steps would you tell him to do?

    When you have those steps written out in English, you'll have an algorithm that should be pretty simple to convert to code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by KevinWorkman View Post
    When you have those steps written out in English, you'll have an algorithm that should be pretty simple to convert to code.
    Don't know why lots of people lazy to do that.

  5. #5
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:18 AM. Reason: code tags added

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  7. #7
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Three parameters? You mean the three arguments? I'm Not sure i understand the question.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  9. #9
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:19 AM.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    If I'm correct, that is basically one is he sentence the other one is what you want to check which is exist or not and the last one is the replacement sentence/word.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    In your code segment, since the next index is validating in each iteration I cannot see that leads to any endless loop.

    Could you post the all three sentences that you used.

  12. #12
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:19 AM.

  13. #13
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    The problem is in your algorithm- you're checking the whole sentence each time, without taking words you've already changed into consideration.

    Are you expecting us to come up with a different algorithm for you? Where's the fun in that?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:19 AM.

  15. #15
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    I see two "easy" ways to do it. I'll give you a hint for each.

    Hint: Why not operate on each word separately, instead of the entire sentence?

    Hint: You want to replace all occurrences of a certain substring with another substring, right? Check out the String API for useful functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  16. #16
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    Thank you, I will see if I can get something to work by looking at it a word at time.

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    Quote Originally Posted by javadingbat View Post
    Thank you, I will see if I can get something to work by looking at it a word at time.
    Sounds like a plan. But just because I feel bad, I'll warn you that the replace all way is a one-liner.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    javadingbat is offline Member
    Join Date
    Feb 2011
    Posts
    19
    Rep Power
    0

    Default

    housekeeping
    Last edited by javadingbat; 02-09-2011 at 11:19 AM.

  19. #19
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @javadingbat,

    Please don't delete the original content in the forum next time. If you did so the thread become really mess, and useless for other members. Please don't do it next time.

Similar Threads

  1. StringBuilder
    By tmotse in forum New To Java
    Replies: 3
    Last Post: 09-13-2010, 08:07 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
  •