Page 2 of 2 FirstFirst 12
Results 21 to 25 of 25
  1. #21
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    You're most welcome!
    Good luck.
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  2. #22
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    One more thing you should know about your program. And the teacher may or may not be aware of it. Let's say you have the following:

    Java Code:
    String m_str ="The moon is full tonight";
    
    replaceAll("moon", "moon");
    What I believe will happen (I haven't tested this) is that it will loop forever. This is because moon is always in the string so the replaceFirst method always returns true.

    A safety feature would be to put something at the beginning of replaceFirst like the following:

    if (find.equals(replace_with))
    return false;

    There may be other gotcha's too. That's why production code needs to go through some pretty extensive testing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #23
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Replacing words using substrings and indexOf

    Quote Originally Posted by jim829 View Post
    One more thing you should know about your program. And the teacher may or may not be aware of it. Let's say you have the following:

    Java Code:
    String m_str ="The moon is full tonight";
    
    replaceAll("moon", "moon");
    What I believe will happen (I haven't tested this) is that it will loop forever. This is because moon is always in the string so the replaceFirst method always returns true.
    It doesn't loop forever; the Sun/Oracle folks took care of that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #24
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,783
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    I believe you are mistaken. We are not talking about the java String routines. He was asked to implement a replaceFirst method which returns either true or false upon success. Then he was asked to write a replaceAll method by successive calls to replaceFirst.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #25
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default Re: Replacing words using substrings and indexOf

    Quote Originally Posted by jim829 View Post
    I believe you are mistaken. We are not talking about the java String routines. He was asked to implement a replaceFirst method which returns either true or false upon success. Then he was asked to write a replaceAll method by successive calls to replaceFirst.

    Regards,
    Jim
    Darryl already notified me about it; I just read your code snippet in your last reply and replied w.r.t. the String.replaceAll( ... ) method; I should've read the entire thread instead; sorry about that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Page 2 of 2 FirstFirst 12

Similar Threads

  1. IndexOf()?
    By Jeron kahyar in forum New To Java
    Replies: 3
    Last Post: 10-15-2012, 07:27 PM
  2. Replies: 5
    Last Post: 04-09-2012, 09:14 PM
  3. Help with indexof please...
    By fatabass in forum New To Java
    Replies: 7
    Last Post: 02-07-2012, 09:36 PM
  4. Split string and joining substrings
    By KarlNorway in forum New To Java
    Replies: 28
    Last Post: 01-27-2012, 10:51 PM
  5. Replies: 9
    Last Post: 08-31-2010, 05:21 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
  •