Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
Name: rlT66838 Date: 08/12/97 import java.io.BufferedReader; import java.io.InputStreamReader; class Test { public static void main(String args[]) throws Throwable { BufferedReader inReader = new BufferedReader(new InputStreamReader(System.in)); String s; while ((s = inReader.readLine()) != null) System.out.println(s); } } Then, typing in "Line 1"..."Line 9" should result in duplicates e.g. Line 1 // input Line 1 // output Line 2 // input Line 2 // output etc. but they come out as e.g. Line 1 // input Line 2 // input Line 1 // output Line 2 // output (this varies?!) ====================================================================== ronan.mandel@Eng 1997-09-09 Another example of this bug he following sample program illustrates the problem (file - Test.java): import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; class Test { public static void main (String[] args) { BufferedReader buf; InputStreamReader inp; buf = new BufferedReader(new InputStreamReader(System.in)); String line = ""; int count = 1; while (!line.equals("exit")) { System.out.print ( count + "> "); System.out.flush(); try { line = buf.readLine(); } catch (IOException ex) { System.out.println (ex.getMessage()); } count++; } } } The program basically just reads and prints lines typed in. If you type slowly, you get the expected behaviour: c:\test>java Test 1> a 2> b 3> c 4> d 5> e 6> f 7> g 8> exit If you enter data quicker you may get, for example, c:\test>java Test 1> a b 2> 3> c 4> d e 5> 6> f g 7> 8> exit The input seems to be buffered, but readLine is not detecting the EOL until another CR-LF is sent. This doesn't seem to happen under Solaris. It may be another manifestation of bug #4071281. company - Sea Change Corporation , email - ###@###.### ============================================================================== Yet another report, which unlike the previous reports asserts that DataInputStream works fine. -- mr@eng 8/3/1998 From: "Mark Cheyne" <###@###.###> To: <###@###.###> Subject: BufferedReader problem Date: Wed, 15 Jul 1998 23:05:38 +0100 I am using jdk1.1.6 and have experienced problems writing programs that form part of the study of Professor Winder and Graham Roberts' book "Developing Java Software" (published by Wiley ISBN 0-471-97655-5). I attach two versions of one trivial program with their compilation and test reports. The program merely reads integers from the keyboard and repeats them back to the screen being terminated with an input zero. The test results of Ex166a (using BufferedReader) are mystifying, whereas the results from Ex166b (using DataInputStream) are as expected. Professor Winder is the head of the Computer Department at King's College and I first referred the problem to him as he suggests using BufferedReader in his book. He confirms that he obtains similar results. However he has found that both programs run as expected on the Solaris platform, and has suggested that the problem may lie with the interface between Window95 and the JVM. ---- import java.io.*; class Ex166a {public static void main (String[] arguments) throws IOException {int inno=1; String inChars=""; BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); while (inno!=0) {inChars=in.readLine(); inno=Integer.parseInt(inChars); System.out.print("Input "+inno+"\n"); } } } /* **** ***** Compilation: C:\King's\Java\programs\Winder>javac Ex166a.java C:\King's\Java\programs\Winder> **** **** Test results C:\King's\Java\programs\Winder>java Ex166a 23 Input 23 34 Input 34 234 Input 234 231 The following lines were not put in rapidly ! but the program seems 345 to get stuck whatever key is struck. 643 7554 4 5 0 434 Input 231 Input 345 Input 643 Input 7554 Input 4 Input 5 Input 0 C:\King's\Java\programs\Winder> */ ---- import java.io.*; class Ex166b {public static void main (String[] arguments) throws IOException, NumberFormatException {int inno=1; String inChars=""; DataInputStream in=new DataInputStream(System.in); while (inno!=0) {inChars=in.readLine(); inno=Integer.parseInt(inChars); System.out.print("Input "+inno+"\n"); } } } /* **** **** Compilation: C:\King's\Java\programs\Winder>javac Ex166b.java -deprecation Ex166b.java:10: Note: The method java.lang.String readLine() in class java.io.Da taInputStream has been deprecated. {inChars=in.readLine(); ^ Note: Ex166b.java uses a deprecated API. Please consult the documentation for a better alternative. 2 warnings C:\King's\Java\programs\Winder> **** **** Test results: C:\King's\Java\programs\Winder>java Ex166b 12 Input 12 234 Input 234 32345 Input 32345 12 Input 12 67 Input 67 2 Input 2 3 Input 3 4 Input 4 67890 Input 67890 1234567 Input 1234567 0 Input 0 C:\King's\Java\programs\Winder> */
|