写一个java程序 要去掉句子中的特定词。用split() 和the indexOf()

结果的效果:
Please enter the filtering key: dog
Please enter the input sentences:
Many people keep animals at home. Cats and dogs are the most popular. Some people keep
snakes, but this is rare. I like dogs the most. They are really cute.
The filtered sentences:
Many people keep animals at home.
Some people keep snakes, but this is rare.
They are really cute.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FilterKeyword {
 /**
  * 
  * @param args
  */
 
 private static String key;
 
 private static String sentences;
 
 private static String[] tempStr;
 public static void main(String[] args) {
  
  BufferedReader inStr = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Please enter the filtering key: ");
  try {
   key = inStr.readLine();
      System.out.println("Please enter the input sentences: ");
      sentences = inStr.readLine();
  } catch (IOException e) {
   e.printStackTrace();
  }
  System.out.println("The filtered sentences: ");
  //key = "dog";
  //sentences = "Many people keep animals at home. Cats and dogs are the most popular. Some people keep snakes, but this is rare. I like dogs the most. They are really cute.";
  tempStr = sentences.split("[.] ");
  for (String temp : tempStr){
   if(temp.indexOf(key)<=0){
    if (temp.indexOf(".")<=0)
        System.out.println(temp + ".");
    else
     System.out.println(temp);
   }
  }
 }
}

温馨提示:答案为网友推荐,仅供参考
相似回答