首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

pushback源的例子

2012-08-30 
pushback流的例子pushback流 有PushbackInputStream和PushbackRead。?例子:?public class SequenceCount {p

pushback流的例子

pushback流 有PushbackInputStream和PushbackRead。

?

例子:

?

public class SequenceCount {public static void main(String[] args) throws IOException {PushbackInputStream in = new PushbackInputStream(System.in);int max  = 0; // longest sequence foundint maxB = -1;// the byte in that sequenceint b;// current byte in inputdo {int cnt;int b1 = in.read(); // 1st byte in sequencefor(cnt = 1; (b = in.read()) == b1; cnt++) {continue;}if(cnt > max) {max = cnt; // remember lengthmaxB = b1; // remember which byte value}in.unread(b); // pushback start of next sequence} while(b != -1); // until we hit end of inputSystem.out.println(max + " bytes of " + (char)maxB);}}

?

?

书上的一个例子;

总结:

1,pushback适用于“词法的扫描”;

2,上面的例子就是找System.in中输入的连续的重复的字符,只有读了之后,才知道不连续重复了。需要回退。

3,在eclipse中不好测试,没法System.in结束,需要在cmd中运行,然后“ctrl+c”结束。打开cmd,切换到eclipse项目的bin目录下面,用java + package.class的名字运行,主要要是class的全路径。

4,读入的是int,如是8的话,assic编码maxB成了58了。需要char强制转换一下,成了8.

热点排行