簡體   English   中英

如何用 ArrayList 中的元素替換字符串變量中的單個單詞?

[英]How do I replace a single word in a String variable with an element in an ArrayList?

ArrayList<String> nounsList = new ArrayList<String>();
ArrayList<String> verbsList = new ArrayList<String>();

(數組列表是從兩個文件的內容創建的)

while((line_2 = wordReader2.readLine()) != null){
  //save all words in nouns.txt to array
  nounsList.add(line_2);
}

while((line_3 = wordReader3.readLine()) != null){
  //save all words in verbs.txt to array
  verbsList.add(line_3);
}

String libStory = "A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument";

Random rand = new Random(); //required to pick random noun in nounsList array
int randElement;

    for (int y = 0; y < libStory.length(); y++) {
  
      randElement = rand.nextInt(nounsList.size());

      libStory.replace("paragraph", nounsList.get(randElement)); 
 

  }//end of for loop

它顯示了一個錯誤(不兼容的類型:int 無法轉換為 String)。

我想做什么:我想從我的字符串變量(libStory)中 select 一個特定的詞(段落),並用另一個詞替換它,該詞是我的 arrayList(名詞列表)中的一個元素。 任何建議如何實現這一目標? 或者如何修復我的代碼來實現這一點? 先感謝您。

編輯:如果我有一個字符串怎么辦(“A % 是一系列相關的句子,形成一個中心 %,稱為 %。嘗試從主題統一性的角度考慮段落:一個段落是一個 % 或一組 % 支持一個中心、統一的想法”),並且每次出現另一個 % 時,我想用數組中的不同元素替換占位符 (%)?

如果您的目標是用列表中的一個隨機元素替換"paragraph"每個精度,則不需要使用 for 循環。 String.replace(CharSequence target, CharSequence replacement)將為您完成。 來自 java 文檔:

用指定的文字替換序列替換此字符串中與文字目標序列匹配的每個 substring。

    ArrayList<String> nounsList = ...

    String libStory = "A paragraph is a series of related ...";
    
    Random rand = new Random();
    
    libStory = libStory.replace("paragraph", nounsList.get(rand.nextInt(nounsList.size())));

    System.out.println(libStory);

只是不要忘記將替換的字符串分配給您的原始字符串,即

libStory = libStory.replace("paragraph",...

根據描述和評論,要將所有出現的“段落”替換為隨機名詞,您可以使用replace()

List<String> nounsList = Arrays.asList("paragraph", "series", "sentences");
String libStory = "A paragraph is a series of related sentences developing a central idea, called the topic. Try to think about paragraphs in terms of thematic unity: a paragraph is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument";

Random rand = new Random(); //required to pick random noun in nounsList array

int randElementIndex = rand.nextInt(nounsList.size());
libStory = libStory.replace("paragraph", nounsList.get(randElementIndex));

System.out.println(libStory);

Output:

A series is a series of related sentences developing a central idea, called the topic. Try to think about seriess in terms of thematic unity: a series is a sentence or a group of sentences that supports one central, unified idea. Paragraphs add one idea at a time to your broader argument

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM