Hi all,

I'm writing a program which will replace certain words in entered text and output the modified sentence.

I've opted to use StringTokenizer to split the sentence, put the words into a string array, use a 'for' loop to replace the necessary words and then output the array once more with spaces in between.

I kept getting exceptions, so I commented out many sections and it boils down to the second half of the array not being output, but instead giving "null" values.

I get no errors / exceptions in the console

Here's a sample of the code that's giving me the trouble:

String enteredText = englishBox.getText();
		//split space-separated entered text into array
		StringTokenizer tok = new StringTokenizer( enteredText, " ");
		//create string array of length countTokens
		String[] sentenceArray = new String[tok.countTokens()];
		//fill sentenceArray with tokens
		for( int j = 0; j < tok.countTokens() ; j++ ){
			sentenceArray[j] = tok.nextToken() ; 
		//output length of array	
		outputBox.setText( Integer.toString( sentenceArray.length ) );
		//output entire array
		for( int i = 0; i < sentenceArray.length; i++ ){
			outputBox.append(" " + sentenceArray[j]);

Note the part where I output the length of the array. This is always correct, but I only ever get exactly half of the array output and the other half in null values.

Any ideas?

I would suggest to use the split method:

String [] sentenceArray = enteredText.split(" ");

According to the API docs, countTokens() returns

the number of tokens remaining in the string using the current delimiter set

So, before you call nextToken on a "tokened String" with 5 elements it returns 5, after calling nextToken once it returns 4, so, what do you think is happening with your for loop?


int size = tok.countTokens();

and use size in both places where you currently use countTokens() .

commented: Good call. I didn't bother to examine it that closely +8

You guys are fantastic.

Thank you for the explanation, Masijade. I would have tried to see what I could do to fix it were it not for the stupendously simple string.split() method, which I used instead and removed all the Tokenizer nonsense.

I started typing another problem to ask you guys about when I noticed something, which led to checking something and which led to not only solving the small problem but getting everything sorted.

My program is now fully functional!

In case anybody wonders, it's a basic GUI which takes a regular English sentence and turns it into Nadsat (the slang type of language used in the book 'A Clockwork Orange').

Thanks again :)