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?