写一个java程序 要去掉句子中的特定词.用split() 和the indexOf()结果的效果:Please enter the filtering key:dogPlease enter the input sentences:Many people keep animals at home.Cats and dogs are the most popular.Some people keepsna
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/28 02:29:25
写一个java程序 要去掉句子中的特定词.用split() 和the indexOf()结果的效果:Please enter the filtering key:dogPlease enter the input sentences:Many people keep animals at home.Cats and dogs are the most popular.Some people keepsna
写一个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.
写一个java程序 要去掉句子中的特定词.用split() 和the indexOf()结果的效果:Please enter the filtering key:dogPlease enter the input sentences:Many people keep animals at home.Cats and dogs are the most popular.Some people keepsna
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);
}
}
}
}