12 Replies - 396 Views - Last Post: 25 June 2013 - 09:45 PM Rate Topic: -----

#1 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Replacing words without replace() method

Posted 23 June 2013 - 09:02 AM

Hello
I need a hint how to put this into a loop for replacing all the "cat" to "dog"
Note that I can't replace cat is scat and Cat.
So my piece of code only replaces the first cat. Any hint will be helpful

Main method
 public static void main(String [ ] args)
    {
        IO.outputStringAnswer(replaceWords("cat", "dog", "a cat scat cat bye Cat"));
     
    }


Replace Words method
 public static String replaceWords(String oldWord, String newWord, String line)
    {
    	int old = line.indexOf(oldWord);
    	if (old < 0)
    	{
    		return line;
    	}
    	
    	String bef = line.substring(0, old);
    	String aft = line.substring(old +oldWord.length());
    	
    	return bef + newWord + aft;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Replacing words without replace() method

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,078
  • Joined: 05-April 11

Re: Replacing words without replace() method

Posted 23 June 2013 - 09:19 AM

So "cat" can be delimited by a whitespace. That is a very important detail
You could build a regex that would match just "cat"
If you don't want to mess around with confusing regex patterns, then split the word
public static String replaceWords(String oldWord, String newWord, String line) {
	String[] words = line.split(" "); //Could be changed to split on every whitespace with \\s
	
	for (int i=0; i<words.length; i++)
		if (words[i].equals(oldWord))
			words[i] = newWord;
	
	...
}


Was This Post Helpful? 1
  • +
  • -

#3 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 10:24 AM

I wished I could use that split method. Life would be easier
Gotta add an extra space end of the line and use while loop to separate the words.
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1391
  • View blog
  • Posts: 3,078
  • Joined: 05-April 11

Re: Replacing words without replace() method

Posted 23 June 2013 - 10:28 AM

what?
Was This Post Helpful? 0
  • +
  • -

#5 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 10:39 AM

You lead me to the right way but I can't use split() method. I have to use some other method like while loop to split the words in every whitespace.

like
line = line + " "

then split the words in the index of white spaces
while (0 < line.length())

and crop the line inside the while loop so it gets shorter in each run.

Thanks, I'll try to figure out from this
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 1954
  • View blog
  • Posts: 4,055
  • Joined: 11-December 07

Re: Replacing words without replace() method

Posted 23 June 2013 - 12:48 PM

So, let's take a stab at your requirements:

Have to do the logic yourself, no replace(), find(), split(), etc. methods from the API.
Replace whole words on a simple text match.
Case sensitive.
Whole words.

I would start by writing my own find() method. It doesn't have to be fast or fancy. It just has to be correct. It should take an int that represents where to start searching and return the index of the start of the found word.

Your find method will need to check if the characters before and after a match are word delimiters. Coding this is easy but deciding what is a delimiter is difficult. Letters are not. Numbers might be or might not be. Some punctuation s a delimiter (.,:;) some shouldn't be a delimiter (-'). What about stiff like +/*# ? It's your call. However you do it, this is a potential point of change in your program. You should isolate it in its own method. Take the String and an index as parameters and return true if it is a delimiter, false otherwise. It would also simplify the code if the the indexes immediately before the start of the string (-1) and after the end (length) count as delimiters.

Once you have written your own find method, you can call it repeatedly to find all the occurrences of the search string. The replace becomes pretty easy.
Was This Post Helpful? 1
  • +
  • -

#7 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 07:17 PM

Ok, I created a loop where it replaces all the old words but how do I return the string with new words in it? Not asking for answer, just little hint helps :D

public static String replaceWords(String oldWord, String newWord, String line)
    {
    	
    	line = line + " ";
    	while (line.length() != 0)
    	{
    		int space = line.indexOf(" ");
        	String firstWord = line.substring(0, space); //Splits the first word
        	if (firstWord.equals(oldWord)) //Checks to see if first word matches oldWord
        	{
        		oldWord = newWord;  //If true oldWord replaces with newWord
        	}
        	line = line.substring(space + 1); //Crops the first word out and loop executes again
        	
    	}
    	return .....
    	
    }


replaceWords("cat", "dog", "a cat scat cat bye Cat")
Input:a cat scat cat bye Cat
Output: a dog scat dog bye Cat
Was This Post Helpful? 0
  • +
  • -

#8 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 07:55 PM

String.split() and String.equalsIgnoreCase() may be particularly useful.
Was This Post Helpful? 0
  • +
  • -

#9 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 08:16 PM

View PostFlukeshot, on 23 June 2013 - 07:55 PM, said:

String.split() and String.equalsIgnoreCase() may be particularly useful.

I can't use those methods.. I'm restricted to loops and stuff
Was This Post Helpful? 0
  • +
  • -

#10 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 08:35 PM

You can't use equalsIgnoreCase() ? You might have a tough time with the last Cat, then.
Was This Post Helpful? 0
  • +
  • -

#11 tarakandi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 04-February 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 08:50 PM

View PostFlukeshot, on 23 June 2013 - 08:35 PM, said:

You can't use equalsIgnoreCase() ? You might have a tough time with the last Cat, then.

No last "Cat" staying as is.. just replacing cat with dog..

Here is an updated code... I'll comment where I need help

 public static String replaceWords(String oldWord, String newWord, String line)
    {
    	
    	line = line + " ";
    	String newline = null;
    	while (line.length() != 0)
    	{
    		int space = line.indexOf(" ");
        	String firstWord = line.substring(0, space); 
        	if (firstWord.equals(oldWord)) 
        	{
        		firstWord = newWord;  
        	}
        	
        	newline = firstWord; //I need help here... I don't know how to build new string from the pieces
        	line = line.substring(space + 1); 
      
    	}
    	return newline; 	
    } 
}

Was This Post Helpful? 0
  • +
  • -

#12 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: Replacing words without replace() method

Posted 23 June 2013 - 08:58 PM

Ah, I see. Then your mistake is down to String immutability. Here is a StackOverflow thread that perfectly describes your situation. :)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Replacing words without replace() method

Posted 25 June 2013 - 09:45 PM

Use the String class split() method using " " as parameter
than it is easy to parse all the token and replace them

String[] token = str.split(" ");

for(int i = 0; i < token.length; ++i) {
   if(token[i].equals("cat"))
      token[i] = "dog";
}
String newString = token[0];
for(int i = 1; i < token.length; ++i)
   newString += " " + token[i];


This post has been edited by pbl: 25 June 2013 - 09:45 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1