代码是这样的:
public void display() throws IOException{
System.out.print("Enter keyword:");
Scanner scan = new Scanner(System.in);
String s =scan.nextLine();
//System.out.println(s.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("d:/record.txt"))); String line = null;
while((line=in.readLine())!=null){
if(line.contains(s));
{System.out.println(line.toString());
}}
意思是这样:
随便输入一个字符串s,看它是否是字符串line的子字符串,若是,这输出line字符串。line字符串是txt文件里一堆的记录。
public void display() throws IOException{
System.out.print("Enter keyword:");
Scanner scan = new Scanner(System.in);
String s =scan.nextLine();
//System.out.println(s.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("d:/record.txt"))); String line = null;
while((line=in.readLine())!=null){
if(line.contains(s));
{System.out.println(line.toString());
}}
意思是这样:
随便输入一个字符串s,看它是否是字符串line的子字符串,若是,这输出line字符串。line字符串是txt文件里一堆的记录。
解决方案 »
- 计算器:输入一段计算式字符串得出运算结果
- 最快的速度找出二维数组的空列并删除
- 谁能指点下spring2.5 与 xfire 整合!
- 问一个排序的算法?
- [100分]很基础的问题:方法的要求的返回值为什么可以用throw new java.lang.UnsupportedOperationException、、、来替代?详细如下
- Heap 和 stack 有什么区别。
- 怎么才能让我的Applet窗体上的可视控件长得跟Windows的风格一样???
- 拜求一个统计的SQL
- JAVA与C++关于JNI中文字符串操作问题???
- 来自java初学者的史上最菜问题之二,期待各位答复
- 初次写java正则表达式,有点晕,小问题谁来看看
- java 如何重新运行程序?
;这行多打了分号,低级错误,debug一跟就出来了。
后面多了个分号;表示满足if条件的话,什么也不做
于是后面的{System.out.println(line.toString());}就和if脱离了关系,变成无条件输出
所以造成结果不对
public class SearchEmp {
public void display() throws IOException{
System.out.print("Enter keyword:");
Scanner scan = new Scanner(System.in);
String s =scan.nextLine();
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("d:/record.txt"))); String line = null;
while((line=in.readLine())!=null){
Pattern p = Pattern.compile(s);
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(line.toString());
} }
}}
输出的结果是我想要的那种,但为什么有时候会有重复存在呢