A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 阿温 中级黑马   /  2014-8-5 08:09  /  654 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1.                 byte[] buf=new byte[1024];
  2.                 InputStream in=System.in;
  3.                 String s;
  4.                 int ch;
  5.                 int point=0;
  6.                
  7.                 while((ch=in.read())!=-1)
  8.                 {
  9.                         if(ch=='\r')continue;
  10.                         if(ch=='\n')
  11.                         {
  12.                                 while(point<1024)buf[point++]=0;
  13.                                 s=new String(buf);
  14.                                 if("over".equals(s))break;
  15.                                 System.out.println(s.toUpperCase());
  16.                                 point=0;
  17.                         }
  18.                         else
  19.                         {
  20.                                 buf[point]=(byte)ch;
  21.                                 point++;
  22.                         }
  23.                 }
复制代码

over.equals()总是判断不相等;感谢@李彦来 同学:这是由于通过buf[]构造的函数"over"后面0也被纳入String中,导致长度不相等.
代码更改如下:
  1.         public static void main(String[] args) throws IOException
  2.         {
  3.                 byte[] buf=new byte[1024];
  4.                 InputStream in=System.in;
  5.                 String s;
  6.                 int ch;
  7.                 int point=0;
  8.                
  9.                 while((ch=in.read())!=-1)
  10.                 {
  11.                         if(ch=='\r')continue;
  12.                         if(ch=='\n')
  13.                         {
  14.                                 s = new String(buf,0,point);
  15.                                 if("over".equals(s))break;
  16.                                 System.out.println(s.toUpperCase());
  17.                                 point=0;
  18.                         }
  19.                         else
  20.                         {
  21.                                 buf[point]=(byte)ch;
  22.                                 point++;
  23.                         }
  24.                 }
  25.         }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马