- byte[] buf=new byte[1024];
- InputStream in=System.in;
- String s;
- int ch;
- int point=0;
-
- while((ch=in.read())!=-1)
- {
- if(ch=='\r')continue;
- if(ch=='\n')
- {
- while(point<1024)buf[point++]=0;
- s=new String(buf);
- if("over".equals(s))break;
- System.out.println(s.toUpperCase());
- point=0;
- }
- else
- {
- buf[point]=(byte)ch;
- point++;
- }
- }
复制代码
over.equals()总是判断不相等;感谢@李彦来 同学:这是由于通过buf[]构造的函数"over"后面0也被纳入String中,导致长度不相等.
代码更改如下:- public static void main(String[] args) throws IOException
- {
- byte[] buf=new byte[1024];
- InputStream in=System.in;
- String s;
- int ch;
- int point=0;
-
- while((ch=in.read())!=-1)
- {
- if(ch=='\r')continue;
- if(ch=='\n')
- {
- s = new String(buf,0,point);
- if("over".equals(s))break;
- System.out.println(s.toUpperCase());
- point=0;
- }
- else
- {
- buf[point]=(byte)ch;
- point++;
- }
- }
- }
复制代码
|
|