java怎么实现 功能_该怎么实现这个功能(java io)

该怎么实现这个功能(java io)

import java.io.*;

public class Test

{

public static void main(String[] args)throws IOException

{

String str,strd;

int count=0;

BufferedReader buf=new BufferedReader(new FileReader("e:\\store.txt"));

BufferedReader buf1=new BufferedReader(new InputStreamReader(System.in));

str=buf1.readLine();

while((strd=buf.readLine())!=null)

{

count++;

if((str.equals(strd))==true)

{

System.out.println("ok");break;

}

else

{

if(count==6)

{

System.out.println("notfound");

}

}

}

buf.close();

这段程序从键盘上输入数据然后与e:\\store.txt里的比较,一样输出“ok”,不一样输出“nofound

”。这个功能可以实现,但是我是在知道e:\\store.txt里面有六行的情况下用的if(count==6),我要是不知道有多少行的情况下应该怎么办。

或者用什么别的方式实现这个功能

----------------解决方案--------------------------------------------------------

把store.txt的文件内容全部进来,然后和你输入的对比不就可以了吗

----------------解决方案--------------------------------------------------------

while((strd=buf.readLine())!=null)

{

count++;

if((str.equals(strd))==true)

{

System.out.println("ok");break;

}

else

{

if(count==6)

{

System.out.println("notfound");

}

}

}

这段不就是吗

----------------解决方案--------------------------------------------------------

当有一行是一样的,你就break了,后面怎么比

----------------解决方案--------------------------------------------------------

这个样子??不行啊.

while((strd=buf.readLine())!=null)

{

if((str.equals(strd))==true)

{

System.out.println("ok");

}

else

{

{

System.out.println("notfound");

}

}

}

打印的结果是

notfound

ok

notfound

notfound

notfound

notfound

我的问题就是解决让所有的行数都找过了之后,如果没有,打印出一个"notfound"

----------------解决方案--------------------------------------------------------

设一个标量,初始时是false,如果有equals就把它设为true,如果全部都读完了,还没有相同的那它就是false了,

然后在while循环外面判断,它是true还是false就可以了

----------------解决方案--------------------------------------------------------

我先试一下,不会在来问阿

----------------解决方案--------------------------------------------------------

程序代码:

import java.io.*;

public class test

{

public static void main(String[] args)throws IOException

{

String str,strd;

int count=0;

BufferedReader buf=new BufferedReader(new FileReader(\"e:\\store.txt\"));

BufferedReader buf1=new BufferedReader(new InputStreamReader(System.in));

str=buf1.readLine();

while((strd=buf.readLine())!=null)

{

count++;

if((str.equals(strd))==true)

{

System.out.println(\"ok\");

buf.close();

return;

}

}

System.out.println(\"notfound\");

buf.close();

}

}

----------------解决方案--------------------------------------------------------

这样的话你count++有什么用?

----------------解决方案--------------------------------------------------------

并且

str.equals(strd))==true)

这个==true是完全多余的

str.equals(strd)它返回的就是一个boolean

----------------解决方案--------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值