-2

I normally could do this, it's very simple code, but I'm having one hell of a brain-fart and I'm eager to figure out my stupidity. I wrote this real quick to try out a new IDE I heard about, and it's supposed to take an inputted string and return it with the words in reverse order, but it keeps on telling me that at some point one of the indices of the String[] have no value.

import java.util.StringTokenizer;  
import java.util.Scanner;  
public class Example {

    /**
     * @param args
     */
    public static void main(String[] args) {

        //set up input
        Scanner input=new Scanner(System.in);
        System.out.println("type something here");
        String sentence=input.nextLine();

        //set up StringTokenizer
        StringTokenizer tokens=new StringTokenizer(sentence);

        //setup for loop
        for(int y=tokens.countTokens()-1; y>=0; y--){
            String[] word={tokens.nextToken()};
            System.out.print(word[y]+" ");
        }





    }

}
user1780932
  • 81
  • 1
  • 7
  • For something so simple, why wouldnt you just run a debugger and step through and watch your variables ? Why waste the time of SO, for such simple programming problems ? Also why have so many blank lines, man its just so immature to put like 6 lines between 2 braces ? – Siddharth Oct 28 '12 at 14:57

4 Answers4

2

The error is here: String[] word={tokens.nextToken()}. You create an array with only one element - the current token. Also countTokens() returns the remaining tokens. It is updated every time, after nextToken() is called.

To fix it: just iterate over all tokens, put them in a list, and then print the list backwards.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
  • I understood that when I looked at my code, I should have been more clear, my annoyance more so lies in the fact that I'm unable to figure out how exactly to fix it. I tried a couple of things that should've worked but, again, I'm experiencing a brain-function glitch of sorts and I seem to have the inability to fix this. – user1780932 Oct 28 '12 at 15:06
1

The problem is String[] word = {tokens.nextToken()}.

I think you want some thing that looks like this :

 int totalTokens = tokens.countTokens()
 String[] word = new String[totalTokens]

 //setup for loop
 for(int y=totalToken-1; y>=0; y--){
     word[y] = tokens.nextToken();           
 }
Karthik Ramachandran
  • 11,925
  • 10
  • 45
  • 53
1

Not sure why your using this StringTokenizer just to reverse a string.

Btw your for loop runs only once and String[] in for loop holds only one element i.e userInput (instead of an array of userInput characters), see if you you can figure out what was wrong in your approach ...

public static void main(String[] args) {

        //set up input
        Scanner input=new Scanner(System.in);
        System.out.println("type something here");
        String sentence=input.nextLine();

        //set up StringTokenizer
        StringTokenizer tokens=new StringTokenizer(sentence);
        String tmpStr = tokens.nextToken();
        int tmp=tmpStr.length();
        for(int y=tmp-1; y>=0; y--){
            char[] word=tmpStr.toCharArray();
            System.out.print(word[y]+ " ");
        }
 }
Coding Mash
  • 3,338
  • 5
  • 24
  • 45
0

This method invoked like next(tokens) should work:

    public static void next (StringTokenizer tokens){
    String next = null;
    if (tokens.hasMoreTokens()){
        next = tokens.nextToken();
        next(tokens);
    } 
    if (next != null){
        System.out.println(next);
    }
}
Dan Iliescu
  • 435
  • 2
  • 7