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: A while-loop is SUPPOSED to replace a substring

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default A while-loop is SUPPOSED to replace a substring

    I notice that my method doesn't work even though I tried to get it's while-loop set-up to do the following things:

    1. Have each token of a string get put into another one
    2. Replace each given-substring (string2) with it's assigned-replacement (string3) as the string's (string1) tokens pass by
    3. Have it's final answer returned as a variable to the main-method


    Here is my code that has each of it's parts with a comment...

         // For the replacement of a substring with another one.
         public static String replaceSubstring(String string1, String string2, String string3)
         {
              Scanner keys = new Scanner(System.in);
     
              // Use a StringTokenizer-object for breaking a string
              // down to the point of finding that matching-substring
              // in general.
              StringTokenizer numOne = new StringTokenizer(string1, " ", true);
              StringTokenizer num1 = new StringTokenizer(string1, " ", true);
              StringTokenizer numWon = new StringTokenizer(string1, " ", true);
     
              String attacher = new String();
     
              // Find the substring with the while-loop.
              while (numOne.hasMoreTokens())
              {
                   if (num1.nextToken() == string2)
                   {
                        attacher += string3;
                   }
                   if (numWon.nextToken() != string2)
                   {
                        attacher += numWon.nextToken();
                   }
              }
     
              // Finally, return the String-object.
              return attacher;
         }

    ...and here is the output-part I want to avoid at runtime.

     

    Exception in thread "main" java.util.NoSuchElementException
    at java.util.StringTokenizer.nextToken(StringTokenize r.java:349)
    at MiscellaneousStr.replaceSubstring(MiscellaneousStr .java:93)
    at MiscellaneousSDemo.main(MiscellaneousSDemo.java:84 )




    So as you can see, I have to avoid getting a message that refers to having no more elements in an enumeration.

    Any suggestions?

  2. The Following User Says Thank You to SOG For This Useful Post:



  3. #2
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: A while-loop is SUPPOSED to replace a substring

    numOne does not "move". You never consume tokens from it. Hence, while's condition is always true. In contrast, numWon and num1 "move" through 1 token in each iteration. Naturally, in an infinite loop, they stumble upon the last element one day. When that day comes, they have no more element to consume and throw exception.

    And, this implementation is a bad practice. You can use just one tokenizer for all your needs, not 3.
    StringTokenizer oneAndOnly = //blahblahblah
     
    while(oneAndOnly.hasMoreTokens()) {
      String next = oneAndOnly.nextToken();
      if(next == string1) attacher += string3;
      else attacher += next; //or oneAndOnly.nextToken(), depending on your needs.
    }

  4. The Following 2 Users Say Thank You to angstrem For This Useful Post:

    SOG (May 29th, 2013)

  5. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: A while-loop is SUPPOSED to replace a substring

     if (num1.nextToken() == string2)

    And use the equals method when comparing String objects.

  6. The Following 2 Users Say Thank You to copeg For This Useful Post:

    SOG (May 29th, 2013)

  7. #4
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: A while-loop is SUPPOSED to replace a substring

    I got past the part where there are too many elements, but I still need help finding the word-replacement substring for the program.

    Here is my current code-setup:

              // Use the while loop to replace the substring.
              while (numOne.hasMoreTokens()) 
              { 
                  String next = numOne.nextToken(); // Use the next-string to not overuse the nextToken-method.
     
                  // If next is the same as string2, then tack on string3 to the attacher-string...
                  if (next == string2) 
                  {
                       attacher += string3;
                  } 
     
                  // ...or else you should tack on the next token of string1/next.
                  else
                  {
                       attacher += next;
                  }
              }

    Are there any suggestions now relative to the logic here?

  8. The Following User Says Thank You to SOG For This Useful Post:


  9. #5
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: A while-loop is SUPPOSED to replace a substring

    The problem I should've mentioned is that the current loop I have now just makes my method return the same string-statement I typed in as a user.

    For example:

    The program's output comes as a command and the mine is the response.

     

    Type in a word to replace in a string for later

    this

    Now type in a word to replace that word with

    that

    Now type that string

    this and that

    Your new string is this: this and that



  10. The Following User Says Thank You to SOG For This Useful Post:


  11. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: A while-loop is SUPPOSED to replace a substring

    if (next == string2)
    Don't use == to compare objects, use the equals method instead.
    Improving the world one idiot at a time!

  12. The Following 2 Users Say Thank You to Junky For This Useful Post:

    SOG (May 29th, 2013)

  13. #7
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: A while-loop is SUPPOSED to replace a substring

    Thanks. I finally got the needed response from the computer with the equals-method's help.

  14. The Following User Says Thank You to SOG For This Useful Post:


Similar Threads

  1. Using a while loop to replace array values
    By sll345 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 18th, 2013, 02:06 AM
  2. Loop to find and replace multiple instances of a char
    By Olympaphibian89 in forum Loops & Control Statements
    Replies: 1
    Last Post: October 25th, 2012, 04:11 PM
  3. Wrong with my Code i think. it's not supposed to be like this :(
    By Nahoyman78 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 8th, 2012, 09:11 AM
  4. Not sure what I'm supposed to do...
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: October 3rd, 2011, 11:13 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM