1 Reply Latest reply: Nov 26, 2006 5:18 AM by 807607 RSS

    Reader returns String "null"

    807607
      I buried this in another recent thread I started in which my OP contained a typed-in synopsis both containing an error and not exhibiting the problem behavior, for which I apologize. Here is syntactically correct working code via copy-paste:
      import java.io.*;
       
      public class Test {
       
        final static BufferedReader
          stdin = new BufferedReader(new InputStreamReader(System.in));
       
        private static String getText() {
       
          String s = "--";  //suffix indicates continuation on next input line
       
          try {
            do
              s = s.substring(0, s.length() - 2) + stdin.readLine();
            while (s.endsWith("--"));
          } catch (IOException e) {
            System.err.println(e);
          }
          return s;
        }
        public static void main(String[] args) {
       
          String s;
       
          do {
            System.out.print("Enter text: ");
            System.out.flush();
            s = getText();
            if (s.equals("null")) {
              System.out.println("\"null\" seen.");
              s = null;  }
          } while (s != null);
        }
      }
      Upon entry of Ctrl-Z, the output is: "null" seen.
      The string "null" is not entered by the user.
      I must implicitly be causing a stringOf(null) someplace but I don't see where.