cannot get String.replace method to work


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: cannot get String.replace method to work

  1. #1
    Join Date
    Mar 2005
    Posts
    21

    cannot get String.replace method to work

    Hi

    I am comparing a letter in a string (random_word) against an input character (c).

    trouble is, my for loop only finds the first occurence of a letter in random_word. If there are more than one of any letter it fails to find it. I thought I would remove a letter from the String each time the for loop is traversed to remove this problem.

    The only trouble is the replace() method for String doesnt work correctly. The class compiles but never replaces the letter in the String. Very odd. Can any experienced folks see a glaring error in my code.

    Also would there be a better way of constructing the for loop to find each occurence of the letter in random_word?

    Many Thanks in advance

    javaStorm

    Here is the code I wrote

    Code:
     /**
         *check for letter in word
         **/    
        public void letterInWord (ArrayList current_word, String random_word, int word_length, String c)
        {
                for(int i=0; i < word_length; i++) //start of FOR loop to check letter exists
              { 
                   System.out.println (random_word.indexOf(c));//DEBUG show value to terminal
                   if(random_word.indexOf(c) == i)
                   {
                   System.out.println ("Success");
                   String replace_char;
                   replace_char = "#";
                   random_word.replace(c ,replace_char);
                   current_word.remove(i);
                   current_word.add(i,c);
                   printCurrentWord(current_word, random_word, word_length);
                   enterLetter(current_word, random_word, word_length);
                   }//end of if loop
                   
               }  //end of for loop

  2. #2
    Join Date
    Feb 2004
    Posts
    541
    The indexOf method has an overloaded form that allows you to specify a start location for the search. This allows you to keep searching for extra characters after you have found the first one. Something like the following...
    Code:
    int fromIndex = 0;
    
    while(random_word.indexOf(c,fromIndex) != -1) //ends if c not contained in string
    {
        //do whatever you want to the string....then
    
        fromIndex = random_word.indexOf(c,fromIndex) + 1; //set new start position
    }

  3. #3
    Join Date
    Mar 2005
    Posts
    21
    Thanks mike

    Its finding the other letters ok! I just need to adjust my other code to insert the right letters into another array!

    Many Thanks

    javaStorm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center