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.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Method to take in a string sentence and reverse the tokens

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Thanked 0 Times in 0 Posts

    Default Method to take in a string sentence and reverse the tokens

    import java.util.StringTokenizer;
    public class StringTokens{
      public String reverseWords(String sentence){
        StringTokenizer tokens = new StringTokenizer(sentence);
        String result = "";
          String[] t = new String[10];
          for(int j = 0; j < sentence.length(); j++){
            t[j] = tokens.nextToken();
          for(int k = sentence.length()-1; k >= 0; k--){
            result = result + t[k];
        return result;

    The code above compiles but then gives me an error saying "java.util.NoSuchElementException" I believe referring to line 12
    t[j] = tokens.nextToken();

  2. #2
    Join Date
    Mar 2012
    United States
    My Mood
    Thanked 33 Times in 31 Posts

    Default Re: Method to take in a string sentence and reverse the tokens

    This error comes from how you have the for loop set up. Lets say that sentence = "hello how are you doing today". The length of sentence would be 29 and your loop would therefore use the numbers 0 thru 28. In this case, this would be a problem on 2 fronts:
    1) There are only 6 tokens in "hello how are you doing today"
    2) The size of t is only 10
    So, once the loop gets to j = 6, the tokens in the sentence have already been added and there are no more left hence the error.
    Last edited by KucerakJM; April 25th, 2012 at 05:16 PM.

Similar Threads

  1. [SOLVED] Method to reverse String using While Loop
    By Montrell79 in forum Loops & Control Statements
    Replies: 2
    Last Post: February 22nd, 2012, 03:49 PM
  2. Reverse every pair of words in a string
    By mulligan252 in forum Collections and Generics
    Replies: 4
    Last Post: October 11th, 2011, 04:29 PM
  3. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  4. How to reverse a string, skiping numbers , and ' ?
    By mlotfi in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 26th, 2011, 05:13 AM
  5. How to reverse a String using java.lang.StringBuilder
    By JavaPF in forum Java SE API Tutorials
    Replies: 0
    Last Post: July 22nd, 2009, 09:42 AM