Results 1 to 11 of 11
  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Java stringtokenizer- making it reverse word

    Here is the problem:The method reverseWords, below, is incomplete. It should take all the tokens from the argument sentence and return a new String, minus any whitespace, with the tokens in reverse order. (Note: my teacher gives the code, I can only fill in the missing part)

    For example, given the String "123 abc 456" the method should return "456abc123".

    Java Code:
    import java.util.StringTokenizer;
    
    public class StringTokens {
    
      public String reverseWords(String sentence) {
        StringTokenizer tokens = new StringTokenizer(sentence);
        String result = "";
        while (tokens.hasMoreTokens()) {
    //I have to fill it in here}
        return result;
      } //end method
    } //end class

    So I made this to test it out:
    Java Code:
    import java.util.StringTokenizer;
    public class StringTokens {
    public static void main(String[] args){
     String sentence = "hi hello";
        StringTokenizer tokens = new StringTokenizer(sentence);
        String result="";
        while (tokens.hasMoreTokens()) {
          tokens.nextToken();
          System.out.println(result+tokens.nextToken());
          
          }
     
      } //end method
    } //end class
    The problem is it only prints out "hello." Why won't it print out "hello hi" ?

  2. #2
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    Within your while loop, you are calling the nextToken() method twice, meaning that it is reading two tokens for each pass through the loop rather than just one. You should store the return value of the first call to nextToken() in a String variable, and refer to that variable later in the loop rather than calling nextToken() a second time.

    Also, you are never changing the value of result, so it will still be "" at the end of the program.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Java stringtokenizer- making it reverse word

    Java Code:
    tokens.nextToken();
    System.out.println(result+tokens.nextToken());
    The first line reads a token ("hi") and throws it away. That is you do nothing with it. Then the next line reads the next token ("hello") and prints it. After that there are no more tokens so the while loop exits. Try using a String with 4,6,8 etc words and you will see what it does.

  4. #4
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    Java Code:
    import java.util.StringTokenizer;
    public class StringTokens {
    public static void main(String[] args){
     String sentence = "hi hello how are you";
        StringTokenizer tokens = new StringTokenizer(sentence);
        String result="";
        while (tokens.hasMoreTokens()) {
          String s=tokens.nextToken();
            System.out.print(s);
          
          }
     
      } //end method
    } //end class
    Prints out: hihellowhowareyou

    this is the hint from the problem:use the StringTokenizer to pull off the tokens, one by one. Then, paste each one on the back of the String variable result.

    I know I have the first part. But Im not sure what the second part wants me to do, since putting result+s will just give me s because right now result just holds empty strings. I'm trying to think about how I use result....I thought about using a for loop, but that would reverse the characters and not the strings.

  5. #5
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    you have to use "result=s+result;" in your while loop so it continually appends s to the beginning of result.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Java stringtokenizer- making it reverse word

    It is recommend that you use String.split instead of StringTokenizer. The split method produces an array which you can use a reverse loop on. Then you can append each word to a StringBuilder. That is how I would do this.

  7. #7
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    Java Code:
    import java.util.StringTokenizer;
    public class StringTokens {
      public static void main(String[] args){
        String sentence = "hi hello how are you";
        StringTokenizer tokens = new StringTokenizer(sentence);
        String result="";
        while (tokens.hasMoreTokens()) {
          String s=tokens.nextToken();
          result=s+result;
          System.out.print(result);
           } 
      } //end method
    } //end class
    this gives me an output of:hihellohihowhellohiarehowhellohiyouarehowhelloh i....crazy looking but at the end its says youarehowhellohi...How can I get it just to print this part? since the while loop prints hi, then hellohi, then howhellohi...until finally it prints youarehowhellohi(which is what I want).

    And Junky, I can't do that since I can only edit the code after while(tokens.hasMoreTokens())...

  8. #8
    DiamondSoul is offline Member
    Join Date
    Oct 2011
    Posts
    83
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    Put the output statement outside of the while loop. You only want to output the result once, not every single time you get a new token.

  9. #9
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    thank you so much DiamondSoul! It worked !! :)
    thanks for pointing out that I need to put the print statement outside the while loop. I should know that by now, I seem to always forget that.

  10. #10
    Join Date
    Sep 2013
    Posts
    1
    Rep Power
    0

    Default Re: Java stringtokenizer- making it reverse word

    Excuse me i have a doubt in your program....
    the output of your program prints the words in a reverse order but with out white spaces.I want the output with spaces...so can u help me to print the output with spaces.for that what are the changes needed to your program...

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

    Default Re: Java stringtokenizer- making it reverse word

    This thread is old and the OP is probably not listening. You should start your own thread on the topic.

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

Similar Threads

  1. Word Count in Java
    By michaelangelokrk in forum New To Java
    Replies: 4
    Last Post: 09-22-2011, 08:34 AM
  2. Replies: 3
    Last Post: 08-30-2011, 04:44 PM
  3. reverse engineer java code
    By gedas in forum New To Java
    Replies: 0
    Last Post: 04-09-2011, 03:06 PM
  4. Java reverse polish stack problem
    By fuzzdn in forum New To Java
    Replies: 0
    Last Post: 12-16-2010, 07:36 PM
  5. Reverse engineer a java code
    By lenny in forum Advanced Java
    Replies: 1
    Last Post: 07-25-2007, 11:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •