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

Thread: Running out of ideas...

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Running out of ideas...

    Hi!

    I started learning Java with The OU and really enjoying it but the assessments are driving me absolutely mad. I'm not considering myself an idiot but some of my brain cells are obviously not functioning properly because I constantly get stuck on the questions, which supposed to be easy...
    Here's my problem:
    I've been asked to write a method allowing replacement of all occurrences of old string within the source string with the new string (my args). I'm only allowed to use protocol from the StringBuilder class. My method works in the way that it finds the occurrences of the old string (using the required method indexOf(Str, int) within the for loop) but it doesn't replace any (using either combination of delete/insert or replace) and it throws index out of bounds exception at me. Reading through the StringBuilder API didn't make me any smarter and I've wasted lots of hours running around in circles.
    My question is: how can I convince the StringBuilder object (source string converted to StringBuilder object) to replace old string with the new one?

    Any hints please? I don't expect anyone to write the code for me just would like to know what I'm doing wrong.

    Many Thanks,
    Cat


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,234
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Running out of ideas...

    Moved to:
    What's Wrong With My Code?

    Recommend you post what you've done so far, without which its hard to give advice on what is wrong.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Running out of ideas...

    Right...

    public static String replaceBuggers(String sourceOne, String oldOne, String newOne)
    {
    StringBuilder searchMe = new StringBuilder(sourceOne);
    int foundStart = 0;
    int foundEnd = 0;
    for (int seek = 0; seek < searchMe.length(); seek++)
    {
    foundStart = searchMe.indexOf(oldOne, seek);
    foundEnd = foundStart + oldOne.length();
    searchMe.replace(foundStart, foundEnd, newOne);
    }

    sourceOne = searchMe.toString();
    return sourceOne;
    }

    This is only one of the methods I've to write and cut out chunks of it which didn't work anyway. I know that I'm missing something simple and obvious but as I am not a Java guru I apologise for being dumb.

    Thanks everyone!
    C

    Thanks!
    Last edited by Cat; March 10th, 2011 at 03:42 PM.

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Running out of ideas...

    How did you come up with the condition for the for loop?
    Also fully read the indexOf() api doc
    Last edited by JJeng; March 10th, 2011 at 05:56 PM.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Running out of ideas...

    Hi,

    After a nearly full night's sleep I started from scratch and still keep swearing at myself for being complete and utter idiot for not solving the puzzle quicker.
    My code is using a while loop now with the condition (searchMe.indexOf(oldOne, seek) != -1).
    Also after checking Java tutorial I figured out that my declaration of foundEnd should have been foundEnd = foundStart + oldOne.length() 0 + 1; whilst replacing statement searchMe.replace(foundStart, foundEnd + 1, newOne);
    Seeing that intensive thinking doesn't always help I shall have a break next time before embarrassing myself again

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Running out of ideas...

    I'm glad you found the issue, I asked you to check the indexOf() api to lead you to what it returned if it found nothing.

    The way I found this bug was to tackle the index out of bounds error, thus I checked out the values of the index with:

    System.out.println(foundStart+" "+foundEnd); //in the for loop

    I then gave it a simple test string of "asdfqwertyasdf" and told it to change "qwerty" to "asdf". It gave me
    4 10
    -1 5

    Before crashing

    After seeing a -1 and that being set by the indexOf() method, I took a look at the api, and noticed it was the return case for not finding the string. That lead to me thinking about the exit case for the loop. seek < searchMe.length() would imply that you were expecting the string to show up length() times. But that would be impractical and we'd want to stop before the -1 is used in a calculation. Thus I questioned you on the loop condition to hopefully lead you in to rethinking when the loop should exit.

    Well I hope I helped, I know my questions seemed very vague, but alot of thought went into them. In the future try using a debugger or strategically placing print() in loops to watch variables in loops.

  7. The Following User Says Thank You to JJeng For This Useful Post:

    Cat (March 12th, 2011)

  8. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    4
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Running out of ideas...

    I got into habit of adding System.out.println() to see how the codes behave and I think it's a great help. I didn't see the -1 as an issue then, which was essentially stopping the code from doing its job after it found all strings. I know that I've been vague but after coming across entire assessments posted on coding forums I didn't want to follow the suit. Especially that I'm more interested in learning how to write a good working code than asking other people to write it for me. Thank you for taking your time to look at it.

Similar Threads

  1. A java beginner needs a lot of help and ideas
    By vesa in forum Java Theory & Questions
    Replies: 1
    Last Post: May 24th, 2010, 09:46 PM
  2. I need ideas on how to read this
    By thursgun in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: April 21st, 2010, 09:31 PM
  3. Any ideas for begginer?
    By SwEeTAcTioN in forum Java Theory & Questions
    Replies: 11
    Last Post: October 27th, 2009, 03:28 AM
  4. Small Project Ideas
    By Freaky Chris in forum Project Collaboration
    Replies: 20
    Last Post: August 12th, 2009, 12:49 PM