S
sasuke
Hello all.
When developing console based applications for helping beginners learn
Java, I have noticed a peculiar problem with the Scanner class, the
utility class which was introduced in Java 5 to ease the activity of
reading from input streams and processing character streams. Its
behaves in a strange manner when presented with a EOF token[CTRL + Z]
or EOT token [CTRL + C] to terminate the program. When presented with
EOF, the program goes into an infinite loop and when presented with
EOT, the program prints out the prompt a good many times before the VM
process is actually terminated. This scenario doesn't appear when a
BufferedReader is used for processing the user input.
To prove this point, I present a sample code which asks the user for
some input and tells him whether his input can be successfully parsed
to a number or not. My development environment is Java 5 release 12 on
a Windows XP SP2 box.
<code>
import java.util.*;
import java.io.*;
public class GGScanTest {
// Pass an argument to start the Scanner Test and run without
// any arguments to run the BufferedReader test
public static void main(final String[] args) {
if(args.length > 0) {
System.out.println("Starting java.util.Scanner test");
new GGScanTest().startTest();
} else {
System.out.println("Starting java.io.BufferedReader test");
new GGScanTest().startBufTest();
}
}
public void startTest() {
Scanner in = new Scanner(System.in);
int i = 0;
while(true) {
System.out.print("Enter something: ");
if(in.hasNext()) {
if(in.hasNextInt()) {
i = in.nextInt();
System.out.println("You entered a number.");
} else {
in.next();
System.out.println("You didn't enter a number.");
}
}
}
}
public void startBufTest() {
BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
int i = 0;
String str = null;
while(true) {
System.out.print("Enter something: ");
try {
str = in.readLine();
if(str == null) System.exit(0);
i = Integer.parseInt(str);
System.out.println("You entered a number.");
} catch(Exception e) {
System.out.println("You didn't enter a number.");
}
}
}
}
</code>
My guess is that when presented with a EOF token or CTRL + C something
messes with the internal state of the Scanner causing it to go
haywire. I couldn't find anything in the Scanner API which could
*reset* this corrupt state or make it EOF/EOT aware. Is this a known
issue with a known workaround/solution?
Comments, explanations most appreciated.
../sasuke
When developing console based applications for helping beginners learn
Java, I have noticed a peculiar problem with the Scanner class, the
utility class which was introduced in Java 5 to ease the activity of
reading from input streams and processing character streams. Its
behaves in a strange manner when presented with a EOF token[CTRL + Z]
or EOT token [CTRL + C] to terminate the program. When presented with
EOF, the program goes into an infinite loop and when presented with
EOT, the program prints out the prompt a good many times before the VM
process is actually terminated. This scenario doesn't appear when a
BufferedReader is used for processing the user input.
To prove this point, I present a sample code which asks the user for
some input and tells him whether his input can be successfully parsed
to a number or not. My development environment is Java 5 release 12 on
a Windows XP SP2 box.
<code>
import java.util.*;
import java.io.*;
public class GGScanTest {
// Pass an argument to start the Scanner Test and run without
// any arguments to run the BufferedReader test
public static void main(final String[] args) {
if(args.length > 0) {
System.out.println("Starting java.util.Scanner test");
new GGScanTest().startTest();
} else {
System.out.println("Starting java.io.BufferedReader test");
new GGScanTest().startBufTest();
}
}
public void startTest() {
Scanner in = new Scanner(System.in);
int i = 0;
while(true) {
System.out.print("Enter something: ");
if(in.hasNext()) {
if(in.hasNextInt()) {
i = in.nextInt();
System.out.println("You entered a number.");
} else {
in.next();
System.out.println("You didn't enter a number.");
}
}
}
}
public void startBufTest() {
BufferedReader in = new BufferedReader(new InputStreamReader
(System.in));
int i = 0;
String str = null;
while(true) {
System.out.print("Enter something: ");
try {
str = in.readLine();
if(str == null) System.exit(0);
i = Integer.parseInt(str);
System.out.println("You entered a number.");
} catch(Exception e) {
System.out.println("You didn't enter a number.");
}
}
}
}
</code>
My guess is that when presented with a EOF token or CTRL + C something
messes with the internal state of the Scanner causing it to go
haywire. I couldn't find anything in the Scanner API which could
*reset* this corrupt state or make it EOF/EOT aware. Is this a known
issue with a known workaround/solution?
Comments, explanations most appreciated.
../sasuke