Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Replacing words using substrings and indexOf

    Can anybody help me start a code which uses only substrings and indexOf to replace words?

    I.E.

    "She Sells Sea Shells Down by the Sea Shore"

    I need to replace both 'Sea' with Lake.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    What have you tried? Look at the String functions and read the API. There should be some insight as to how to get started. You should also look at StringBuilder. And use an occasional print statement to verify what you are trying to do at different steps in the process.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    I've been reading through my 'Absolute Java' textbook and I'm totally lost. I'm trying to find a word and then replace it with only indexOf and substring. I've tried many things but have always deleted it because I get frustrated.

  4. #4
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    This is where I'm at so far

    Java Code:
           // This routine finds the first occurrence of the string "find".
        //        Once found, the find string is replaced with the 
        //        "replace_with" string
        //  If "find" is not found in m_str, then false is returned.
        //       Otherwise, true is returned.
        //  HINT: your solution should use the indexOf and substring String methods. 
        //  DO NOT use other methods in the String class that were not covered in our class.
        
        public boolean replaceFirst(String find, String replace_with)
        {
        	int position= m_str.indexOf(find);
        	String ending = m_str.substring(position);
        
        	m_str = m_str.substring(0, position) + replace_with + ending;
        
    	  }

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    Where is m_str coming from or where is it defined?

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    Edited
    Last edited by LetsG0Blue; 03-02-2013 at 03:57 AM. Reason: Personal

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    OK, here is a test case I did with your code and some print statements.

    Java Code:
    public class Testit {
       // This routine finds the first occurrence of the string "find".
       // Once found, the find string is replaced with the
       // "replace_with" string
       // If "find" is not found in m_str, then false is returned.
       // Otherwise, true is returned.
       // HINT: your solution should use the indexOf and substring String methods.
       // DO NOT use other methods in the String class that were not covered in our
       // class.
       String m_str = "Now is the time for all good men to come to the aid of their country.";
       public static void main(String[] args) {
          Testit t = new Testit();
          System.out.println(t.m_str);
          if (t.replaceFirst("men", "women")) {
             System.out.println("Success!");
             System.out.println(t.m_str);
          }
       }
    
       public boolean replaceFirst(String find, String replace_with)
       {
           int position= m_str.indexOf(find);
           String ending = m_str.substring(position + replace_with.length());
           m_str = m_str.substring(0, position) + replace_with + ending;
            
           return true;
         }
    }
    It prints out:

    Now is the time for all good men to come to the aid of their country.
    Success!
    Now is the time for all good womeno come to the aid of their country.

    --------------------------------
    Looks like one of your positions is off. I will also point out that your replaceFirst method always returns true.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    How do I link that to my code on the bottom? (the code is a template then we fill in the desired parts)

    it's output is suppose to be

    Starting with: She Sells Sea Shells Down by the Sea Shore
    Number of Vowels=11
    Number of Consonants=23
    Number of s's = 7
    Result: She Sells Lake Shells Down by the Lake Shore
    ************************
    Starting with: 4 Score and 7 Years Ago
    Number of Vowels=7
    Number of Consonants=9
    Number of a's = 3
    Result: Four Score and Seven Years Ago

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    Your not supposed to link the code to yours. I simply isolated the problem method to help you debug it. Your replaceFirst method still has several problems.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    Can you help identify the problems? I'm just not seeing what's left. I'm not asking you to do it or anything, but what would be the next few steps that I should take?

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    Okay, first you are returning true regardless of the outcome of the replaceFirst method. So if you can't find the desire string to replace, return false. Second, you had the correct idea of adding the string length to the position to find out where to start the "ending string." But you need to use the length of what you are trying to find, not what you are replacing it with.

    e.g.

    Java Code:
    public boolean replaceFirst(String find, String replace_with)   {
           int position= m_str.indexOf(find);  // if this is negative (find not found), return false.
           String ending = m_str.substring(position + replace_with.length());  // should be  + find.length() not replace_with.length()
           m_str = m_str.substring(0, position) + replace_with + ending;
             
           return true;
         }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    I've looked over my code and went back through the chapter about strings in the book. I've tried a few things that ultimately didn't work. I wish my professor would've went over this subject matter before assigning us a project on it. You don't have to show me but any help would be awesome; I have to turn it in tonight. I feel like I'm stupid for having to ask so many questions.

    Java Code:
    public boolean replaceFirst(String find, String replace_with)   {
    
    
        	int position= m_str.indexOf(find);  
        	if (find.equals(0)) {
        		return false;
        	}
        	else {
        		String ending = m_str.substring(position + find.length());  
        		m_str = m_str.substring(0, position) + replace_with + " " + ending;;   
        		return true;
        	}
        }

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    First, everyone asks questions. That is the purpose of the forum.


    You are almost there. It is not find.equals(0) that you want it is position. If position < 0 then the search failed.

    Also, don't add the extra " " in the construction of the new string.

    Make those changes and see what happens.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    Java Code:
        // This routine replaces all occurrences of the "find" String
        //       with the "replace_with" String.  
        //  HINT: your solution should  call replaceFirst and continue as long
        //      as replaceFirst returns true. 
        // DO NOT use other methods in the String class that were not covered in our class. 
     
        public void replaceAll(String find, String replace_with)
        {
            // ***** Fill in your code 
        }
    Now all I have to do is call upon replaceFirst to have the words replaced?

  15. #15
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    Pretty much. You have a routine which will do a single replacement and returns true or false upon success. So just put replaceFirst inside a loop and put that in replaceAll(). Then keep looping until it returns false. Then return from replaceAll().

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    How do I return the replaceFirst since it's a boolean expression?

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    You don't. replaceFirst should be done. So
    Java Code:
    public void replaceAll(String find, String replaceWith) {
    {
         while (replaceFirst(find, replaceWidth)); // keep doing it while true. Exit loop when false.
    }
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    So.. there isn't any content in the while? I was trying to call replaceFirst inside of the while loop.

    I think I may make things more confusing for myself sometimes.

    Thank you Jim.

  19. #19
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Replacing words using substrings and indexOf

    You could have done it that way too. For example:

    Java Code:
    public void replaceAll(String find, String replace_with) {
        while (true) {
            if (replaceFirst(find, replace_with) == false) 
                  return;
        }
    }
    There is more than one way to accomplish a task when writing code. That's what makes it fun!!

    Jim

  20. #20
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Replacing words using substrings and indexOf

    I love programming! I wish I had less responsibilities outside of it too study more in depth into it! It's hard to find time to really get into something when your married, work 50 hours, and attend college for 15 credit hours. I'm on paid vacation at the end of March, so I think i'll really go through the harder chapters again.

    Thank you very much for helping me!

Page 1 of 2 12 LastLast

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
  •