2017-04-06 28 views
2

想用「*」替換字符串中的所有3個字母的單詞,例如,如果有人輸入「我忘了問」,它會輸出「我忘了***」,這就是我所擁有的,不知道該從哪裏出發。使用StringTokenizer將3個字母的單詞替換爲Asteriks?

public static void main (String[] args) 
{ 
    c = new Console(); 


    c.print("Enter a string: "); 
    String phrase; 
    phrase = c.readLine(); 

    StringTokenizer phrase = new StringTokenizer (phrase); 
    while (phrase.hasMoreTokens()) 
    { 
     c.print (phrase.nextToken()); 

    } 



} // main method 
+0

檢查單詞的'.length()',如果是3打印'***'else print word? – Pshemo

+1

'String phrase = c.readLine()。replaceAll(「\\ D {3}」,「***」);' –

+0

我會把那行放在c.print? – SoloTriesToLearn

回答

0

有趣的問題。我解決了使用Java 8你的問題,我只是增加了一個簡單的測試,證明它的工作原理:

import org.junit.Test; 
import static java.util.Arrays.asList; 
import static java.util.stream.Collectors.joining; 
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.core.Is.is; 

public class SoloTriesToLearn { 

    public String wordReplacer(String sentence) { 

     return asList(sentence.split(" ")).stream() 
       .map(word -> word.length() == 3 ? "***" : word + " ") 
       .collect(joining()); 

    } 

    @Test 
    public void shouldReplaceWordsWithAsterisk() { 
     String result = wordReplacer("I forgot to ask"); 
     assertThat(result, is("I forgot to ***")); 
    } 
} 

所以基本上我在做什麼先被分割的話,那麼我流他們,我做檢測的映射如果長度是== 3,然後我返回***。在流的末尾,我通過連接將元素收回到單個字符串。

我希望你覺得這段代碼有用,我發現這種操作非常容易實現。

更新 好了,如果你的老師不理解Java8,這裏就不擔心使用標記生成你想要的調皮方式;

import org.junit.Test; 
import java.util.StringTokenizer; 
import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.core.Is.is; 

public class SoloTriesToLearn { 

    public String wordReplacer(String sentence) { 

     StringTokenizer st = new StringTokenizer(sentence); 
     String acumulator = ""; 

      while (st.hasMoreElements()) { 
       String currentWord = (String) st.nextElement(); 
       if(currentWord.length() == 3) { 
        acumulator += "***"; 
       } else { 
        acumulator += currentWord + " "; 
       } 
      } 
     return acumulator; 
    } 

    @Test 
    public void shouldReplaceWordsWithAsterisk() { 
     String result = wordReplacer("I forgot to ask"); 
     assertThat(result, is("I forgot to ***")); 
    } 
} 
+0

感謝您的迴應,這是一個在線複選標記類,它應該可以幫助我學習Java,有沒有一種方法可以讓您知道如何使用標記器來執行此操作? – SoloTriesToLearn

+0

@SoloTriesToLearn好吧,當然。檢查更新。我剛剛添加了你使用Tokenizer的代碼。 – sfrj

+0

我試着運行該代碼,並得到10個錯誤?如果這有所作爲,那麼這個班級正在使用「準備編程Java IDE」?如果它會更容易,我可以告訴你我們得到的筆記嗎? @sfrj – SoloTriesToLearn

-1
public static void main (String[] args) 
{ 
    Scanner input = new Scanner(System.in); 

    System.out.println("Enter a string: "); 
    String phrase; 
    phrase = input.nextLine(); 
    ArrayList<String> sentence = new ArrayList<>(); 

    StringTokenizer tokenizer = new StringTokenizer(phrase); 
    while (tokenizer.hasMoreTokens()) 
    { 
     sentence.add(tokenizer.nextToken()); 
    } 
    for (String word : sentence) 
    { 
     if (word.length() == 3) 
     { 
      word = "***"; 
     } 
     System.out.print(word + " "); 
    } 


} // main method 
0

或者只):

phrase = phrase.replaceAll("\\b[a-zA-Z]{3}\\b", "***"); 
相關問題