|
From: <no...@so...> - 2002-04-23 21:34:15
|
Bugs item #547798, was opened at 2002-04-23 14:34 You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=438935&aid=547798&group_id=44253 Category: None Group: Would be nice if fixed ... Status: Open Resolution: None Priority: 5 Submitted By: Nobody/Anonymous (nobody) Assigned to: Nobody/Anonymous (nobody) Summary: problem with interactions window Initial Comment: The following source code compiles OK with version 1.4 of JDK (and also the latest version of DrJava), However, it will not work correctly in the interactions window of DrJava, but does work correctly using the command line prompt in DOS. The error message I receive is: edu.rice.cs.drjava.model.repl.InteractionsException: Also, when you run the program, the cursor is at the beginning, not at the end of the prompt: Enter your name: ==================================================== // From the errata of the book "Beginning Java // Objects" by Jacquie Barker // KeyboardInput.java // I discovered that you can only read one character from the keyboard // at a time, so I created this little class to hide the "ugliness". // // To use it, in your main program, do the following: // // KeyboardInput k = new KeyboardInput(); // k.readLine(); // // then, k.getKeyboardInput(), which returns a full line's worth // of keyboard input, as follows: // // String name = new String(k.getKeyboardInput()); import java.io.*; public class KeyboardInput { // Whatever the user last typed gets saved in this String. private static String keyboardInput; public String readLine() { char in; // Clear out previous input. keyboardInput = ""; try { // Read one integer, and cast it into a character. in = (char) System.in.read(); // Keep going until we reach a newline character (\n). while (in != '\n') { keyboardInput = keyboardInput + in; in = (char) System.in.read(); } } catch (IOException e) { // Optional error reporting goes here. // Reset the input. keyboardInput = ""; } // Strip off any leading/training white space. keyboardInput = keyboardInput.trim(); return keyboardInput; } public String getKeyboardInput() { return keyboardInput; } } //KeyboardInputTest.java import java.util.*; import java.io.*; public class KeyboardInputTest { public static void main(String args[]) { KeyboardInput k = new KeyboardInput(); // Gather input from the keyboard. System.out.print("Enter your name: "); k.readLine(); System.out.println("You typed: |" + k.getKeyboardInput () + "|"); } } ---------------------------------------------------------------------- You can respond by visiting: http://sourceforge.net/tracker/?func=detail&atid=438935&aid=547798&group_id=44253 |