public class IO { // The main method here serves just to test the readLn() method. public static void main(String [] args) { String lineIn = readLn(); System.out.println(lineIn); } // This is here just in case a user of this class tries to make an // instance of it. That is not necessary, since everything here is static. // But it might be done if a different handle on these methods was desired. IO() { } // read a line of input from standard input public static String readLn() { //set aside memory for the keyboard input byte [] byteArray = new byte[512]; //things which could fail should be enclosed // in a try/catch statement try { // read stdin into the byte array. System.in.read(byteArray); } catch (Exception e) { // if there is a failure, print an error e.printStackTrace(); } //make a String from the array of bytes String result = new String(byteArray); //check for a return character, and only //keep the characters up to the return int newLineIndex = result.indexOf("\n"); result = result.substring(0,newLineIndex); return result; } public static String readLn(String prompt) { System.out.print(prompt); return readLn(); } }