下面是我可能會這樣做的。不是你想到的,但我想告訴你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));
}
}
這不是使用程序邏輯的例外是個好主意。
StringTokenizer
是一個JDK 1.0老式類。它經受了時間的考驗,但我不會推薦回到1995年。
非常小的編碼器。換句話說,「請爲我編碼這個簡單的方法。」有人可能,但如果你嘗試一些,你會從中得到更多。 – duffymo
我不確定,但會工作嗎? catch(NoSuchElementException e){return false; } – TheLittleCoder
JVM是你的朋友。你不能因爲膽小而成爲程序員。運行代碼;看看它是否符合你的要求。如果沒有,修復它直到它。這裏有一個提示:在任何時候你都不會將你獲得的標記數量與你傳入的值進行比較。除非迭代了標記數量,否則絕不會拋出異常。還有一個你可能感興趣的countTokens方法。 – duffymo