Here's how I might do it. Not what you had in mind, but I wanted to show you JUnit.
StringUtils.java:
package utils;
import java.util.Arrays;
import java.util.List;
/**
* @author Michael
* @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
*/
public class StringUtils {
private StringUtils() {}
public static List<String> tokenize(String str) {
String [] tokens = new String[0];
if (isNotBlankOrNull(str)) {
str = str.trim();
tokens = str.split("\s+");
}
return Arrays.asList(tokens);
}
public static boolean isBlankOrNull(String s) {
return ((s == null) || (s.trim().length() == 0));
}
public static boolean isNotBlankOrNull(String s) {
return !isBlankOrNull(s);
}
public static boolean hasSufficientTokens(int numTokens, String str) {
return (numTokens >= 0) && tokenize(str).size() >= numTokens;
}
}
StringUtilsTest.java:
package utils;
import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by Michael
* Creation date 12/6/2016.
* @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856
*/
public class StringUtilsTest {
@Test
public void testIsNotBlankOrNull_NullString() {
Assert.assertFalse(StringUtils.isNotBlankOrNull(null));
}
@Test
public void testIsNotBlankOrNull_EmptyString() {
Assert.assertFalse(StringUtils.isNotBlankOrNull(""));
}
@Test
public void testIsNotBlankOrNull_BlankString() {
Assert.assertFalse(StringUtils.isNotBlankOrNull(" "));
}
@Test
public void testIsNotBlankOrNull_FullString() {
Assert.assertTrue(StringUtils.isNotBlankOrNull("I'm not null, blank, or empty"));
}
@Test
public void testTokenize_NullString() {
// setup
List<String> expected = Collections.EMPTY_LIST;
// exercise
List<String> actual = StringUtils.tokenize(null);
// assert
Assert.assertEquals(expected, actual);
}
@Test
public void testTokenize_EmptyString() {
// setup
List<String> expected = Collections.EMPTY_LIST;
// exercise
List<String> actual = StringUtils.tokenize("");
// assert
Assert.assertEquals(expected, actual);
}
@Test
public void testTokenize_BlankString() {
// setup
List<String> expected = Collections.EMPTY_LIST;
// exercise
List<String> actual = StringUtils.tokenize(" ");
// assert
Assert.assertEquals(expected, actual);
}
@Test
public void testTokenize_FullString() {
// setup
List<String> expected = Arrays.asList("I'm", "not", "null,", "blank,", "or", "empty");
// exercise
List<String> actual = StringUtils.tokenize(" I'm not null, blank, or empty ");
// assert
Assert.assertEquals(expected.size(), actual.size());
Assert.assertEquals(expected, actual);
}
@Test
public void hasSufficientTokens_NegativeTokens() {
// setup
int numTokens = -1;
String str = " I'm not null, blank, or empty ";
// exercise
// assert
Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str));
}
@Test
public void hasSufficientTokens_InsufficientTokens() {
// setup
String str = " I'm not null, blank, or empty ";
int numTokens = StringUtils.tokenize(str).size() + 1;
// exercise
// assert
Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str));
}
@Test
public void hasSufficientTokens_NullString() {
// setup
String str = "";
int numTokens = StringUtils.tokenize(str).size();
// exercise
// assert
Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str));
}
@Test
public void hasSufficientTokens_Success() {
// setup
String str = " I'm not null, blank, or empty ";
int numTokens = StringUtils.tokenize(str).size();
// exercise
// assert
Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str));
}
}
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…