Buzz Code
Published in

Buzz Code

Java 8 | StringTokenizer 2+ while

Hi guys! Today I’m going to talk more about the StringTokenizer and the while statement which is another loop. The first post about StringTokenizer is on below.

[while & hasMoreTokens()]

Last time I used countTokens() to count how many tokens are there in the StringTokenizer. There are different way to check if the StringTokenizer has tokens.

while statement is a loop like the for statement, but the difference between them, is that we put initial value, condition, and increment/decrement in the for loop, but in the while statement we can only put the condition to make it repeat itself.

Here, I put the hasMoreTokens() method, and this method checks if there are tokens left in the StringTokenizer. If there are tokens, it returns true, if there’s not it returns false. The way to take the tokens out is the nextToken() method that we used on the last post.

[Split tokens include delimiter]

To make the delimiter into tokens as well, you can simply put the true as the third parameter. Then, as you see, there are tokens that have the delimiter in it. If you put false at the third parameter, it will return the same result with not putting third parameter!

So that’s all for today guys! I’ll bring some fun practice to do on the next post. Thank you for reading it! See ya!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store