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

1个回答

  • 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);

    }

    }

    }

    }