1
這裏特定的詞是主要的:查找/打印出來開始以特定字母
public class MiscStringOperationsDriver
{
public static void main(String[] args)
{
// Test the wordCount method
int numWords = MiscStringOperations.wordCount("There are five words here.");
System.out.printf("Number of words: %d\n\n", numWords);
// Test the arrayToString method
char[] letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g'};
String lettersToString = MiscStringOperations.arrayToString(letters);
System.out.printf("The string is: %s\n\n", lettersToString);
// Test the mostFrequent method
char mostFrequentChar = MiscStringOperations.mostFrequent("aababbcddaa");
System.out.printf("The most-frequent character is: %c\n\n", mostFrequentChar);
// Test the beginWithB method
String wordList = MiscStringOperations.beginWithB(
"Batman enjoyed some blueberries and a bacon burger in the Batmobile.");
System.out.printf("The list of words is: %s\n\n", wordList);
}
}
所有的方法在另一類。我正在用beginWithB方法的最後一個方法掙扎。我有一切工作。這是我到目前爲止對這個方法:
public static String beginWithB(String wordlist) {
String myStr = wordlist;
for(String b: myStr.split(" ")){
if(b.startsWith("b")||b.startsWith("B")){
System.out.print(b);
}
}
我努力尋找一種方法,返回一個「B」或「B」到主開始的話。有任何想法嗎? (是的,我必須這樣做)。
你只是打印B'的'值。您需要將它們添加到列表並返回以b開頭的字符串列表。 'wordList'實際上應該是'List',而不是'String' – TheLostMind
你在返回form'beginWithB()'嗎?你的完整方法是?因爲你想要一個字符串列表,你不需要'string []'從那個方法? –