I am making a function that returns a Boolean type of whether a String has enough tokens. I do this by using this code:
public boolean isEnoughTokens(int tokens, String problem) {
try {
StringTokenizer token = new StringTokenizer(problem);
return true;
} catch (NoSuchElementException ) {
}
}
The problem is that I haven't figured out how to catch a No such element exception. I think it's super simple but still didn't figure out how to do it.
Thanks, any help will be appreciated!!!
Here's how I might do it. Not what you had in mind, but I wanted to show you JUnit.
StringUtils.java:
StringUtilsTest.java:
It's not a good idea to use exceptions for program logic.
StringTokenizer
is a JDK 1.0 vintage class. It's stood the test of time, but I would not recommend going all the way back to 1995.I think I found the answer to my own question! I was messing around and thanks to you informing me about the countTokens() function I came up with this!
I don't know if there is any error but so far when I tested it, it works!