when u use StringTokenizer, in the for loop if u write for( int i =0; i< st.countTokens(); i++) then u may get wrong answer, coz st.countTokens() is variable. In the for loop, u will call st.nextToken(), then after that, st.countTokens() minus 1 already. So u may get null use your program. :st.countTokens() is always count the tokens left.