Java does not have any intrinsic classes for standard input and standard output. David Kosbie, coach of Sewickley Academy's teams, has provided some code that can do what is needed. These two I/O packages will be supplied to contestants at our competition as files global_io.java and exceptn_io.java, so contestants will not need to type them in. I suggest that competitors who intend to program in Java try out the code before coming to our competition. Here is Mr. Kosbie's description of the code he provided:
It's a very, very simplified I/O package -- as I've written it here, it doesn't even use a class of its own, but is included directly in another class (as the test code demonstrates). Some folks really like this approach, others hate it (oh well). Also, I'm including two versions--one uses a global (class variable) boolean to indicate eof, the other uses exceptions (more Java-ish, to be sure, but also confusing to some first-year programmers)...
When you do make this available to everyone, though, you might want to explain...that it is slightly quirky...one version uses a static class variable to indicate end-of-file, which is odd, and the other uses a generic Exception, and so cannot distinguish end-of-file from, say, a misformatted integer (reason being that in the SRU contest this distinction is never necessary -- all input is correctly formatted). The quirks exist as I tried to come up with the smallest...I/O package that satisfied the demands of the SRU contest and would also be most-widely understood by the broadest group of beginner programmers (hence the non-exception based version, and also hence the generic exception version). Also, this explains why the I/O code is not in its own class -- it's easier to paste code into a class than to deal with package and classpath issues for a second class in its own file, plus this works for those students who have not yet learned about classes (another quirk, but again with some good reasons...).
Oh, of course, this code is to be used as-is with no guarantees of correctness and so forth. :-)
Use this package if you haven't learned exceptions yet.
import java.io.*;
// Very Simple I/O Package: Global-based
// File global_io.java
// This version uses a global boolean, "eof", to indicate the end-of-file.
// It must be checked *after* each call to any one of:
// readLine(), readString(), readInt(), readDouble()
// if "eof" is true, ignore the last result and end the program.
public class Test {
public static void test1() {
// read lines until eof
String line;
while (true) {
line = readLine();
if (eof) break;
System.out.println("line: '" + line + "'");
}
}
public static void test2() {
// read whitespace-separated strings until eof
String s;
while (true) {
s = readString();
if (eof) break;
System.out.println("string: '" + s + "'");
}
}
public static void test3() {
// read integers until eof
int i;
while (true) {
i = readInt();
if (eof) break;
System.out.println("int: '" + i + "'");
}
}
public static void test4() {
// read doubles until eof
double d;
while (true) {
d = readDouble();
if (eof) break;
System.out.println("double: '" + d + "'");
}
}
public static void main1(String[] args) {
test4();
}
public static boolean eof = false;
public static BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
// (readChar() == (char)-1) at end of file
public static char readChar() {
char c = (char)-1;
if (!eof)
try { c = (char) input.read(); } catch (Exception e) { }
if (c == (char)-1) eof = true;
return c;
}
// (readString() == "") at end of file
public static String readString() {
char c;
while (true) {
c = readChar();
if (!Character.isWhitespace(c))
break;
}
if (eof) return "";
String s = "" + c;
while (true) {
c = readChar();
if (Character.isWhitespace(c))
break;
if (eof) { eof = false; break; }
s += c;
}
return s;
}
public static int readInt() {
try { return Integer.parseInt(readString()); }
catch (Exception e) { return 0; }
}
public static double readDouble() {
try { return Double.parseDouble(readString()); }
catch (Exception e) { return 0; }
}
// (readLine() == null) at end of file
public static String readLine() {
String s = null;
try { s = input.readLine(); } catch (Exception e) { }
if (s == null) eof = true;
if (eof) return "";
return s;
}
}
Uses exceptions.
import java.io.*;
// Very Simple I/O Package: Exceptions-based
// File exceptn_io.java
// This version uses exceptions to indicate the end-of-file.
// You may call any one of:
// readLine(), readString(), readInt(), readDouble()
// Enclose each call in a try/catch, and the catch code will
// be executed at end of file (assuming no other exceptions occur).
public class Test {
public static void test1() {
// read lines until eof
int count = 0;
try {
while (true) {
String line = readLine();
count++;
System.out.println("line: '" + line + "'");
}
}
catch (Exception e) {
// we get here at end of file
System.out.println("total lines: " + count);
}
}
public static void test2() {
// read whitespace-separated strings until eof
int count = 0;
try {
while (true) {
String s = readString();
count++;
System.out.println("string: '" + s + "'");
}
}
catch (Exception e) {
// we get here at end of file
System.out.println("total strings: " + count);
}
}
public static void test3() {
// read integers until eof
int count = 0;
try {
while (true) {
int i = readInt();
count++;
System.out.println("int: '" + i + "'");
}
}
catch (Exception e) {
// we get here at end of file
System.out.println("total ints: " + count);
}
}
public static void test4() {
// read doubles until eof
int count = 0;
try {
while (true) {
double d = readDouble();
count++;
System.out.println("double: '" + d + "'");
}
}
catch (Exception e) {
// we get here at end of file
System.out.println("total doubles: " + count);
}
}
public static void main(String[] args) {
test4();
}
public static BufferedReader input =
new BufferedReader(new InputStreamReader(System.in));
// readChar() throws Exception at end-of-file
public static char readChar() throws Exception {
char c = (char) input.read();
if (c == (char)-1) throw new Exception();
return c;
}
// readString() throws Exception at end-of-file
public static String readString() throws Exception {
char c;
while (true) {
c = readChar();
if (!Character.isWhitespace(c))
break;
}
String s = "" + c;
while (true) {
try { c = readChar(); }
catch (Exception e) { return s; }
if (Character.isWhitespace(c))
break;
s += c;
}
return s;
}
// readInt() throws Exception at end-of-file
public static int readInt() throws Exception {
return Integer.parseInt(readString());
}
// readDouble() throws Exception at end-of-file
public static double readDouble() throws Exception {
return Double.parseDouble(readString());
}
// readLine() throws Exception at end-of-file
public static String readLine() throws Exception {
String s = input.readLine();
if (s == null) throw new Exception();
return s;
}
}
Go back to Competition Strategies.
Go back to SRU Programming Competition Home Page.