Help: Eof

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zeny
    New Member
    • Jul 2006
    • 44

    #1

    Help: Eof

    Hi everybody,

    My program reads user commands from the console until it finds an EOF. My question is: how does the program knows that an EOF has been inserted? And how does a user introduces an EOF as input through the console?

    I´m using the method below to read the input:

    static String readLn (int maxLg)
    {
    byte lin[] = new byte[maxLg];
    int lg = 0, car = -1;
    String line = "";
    try
    {
    while (lg < maxLg)
    {
    car = System.in.read ();
    if ((car < 0) || (car == '\n'))
    break;
    lin[lg++] += car;
    }
    }
    catch (IOException e)
    {
    return (null);
    }
    if ((car < 0) && (lg == 0)){
    return (null); // eof
    }

    return (new String (lin, 0, lg));
    }


    Thus, to read the input the program calls that method, readLn(). For example:
    String input = readLn(1024).tr im();. From this example how do i know that the string "input" is an EOF? I´d be much thankful for any help.

    Best Regards
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Originally posted by zeny
    Hi everybody,

    My program reads user commands from the console until it finds an EOF. My question is: how does the program knows that an EOF has been inserted? And how does a user introduces an EOF as input through the console?

    I´m using the method below to read the input:

    static String readLn (int maxLg)
    {
    byte lin[] = new byte[maxLg];
    int lg = 0, car = -1;
    String line = "";
    try
    {
    while (lg < maxLg)
    {
    car = System.in.read ();
    if ((car < 0) || (car == '\n'))
    break;
    lin[lg++] += car;
    }
    }
    catch (IOException e)
    {
    return (null);
    }
    if ((car < 0) && (lg == 0)){
    return (null); // eof
    }

    return (new String (lin, 0, lg));
    }


    Thus, to read the input the program calls that method, readLn(). For example:
    String input = readLn(1024).tr im();. From this example how do i know that the string "input" is an EOF? I´d be much thankful for any help.

    Best Regards
    I'm pretty sure the concept is the same as C/C++ (please correct me if I'm wrong!), so that's what the rest of this post will be drawing from.

    EOF is a sentinel value that is interpreted by a compiler. I know in GCC, last time I printed it out, it was -1 when assigned an int value - similar to how strings are terminated with '\0' (once again, certain types of strings - I believe C strings in this case). It's just a representation of some arbitrary, but known, value.

    But that is why you can make the 'stream.eof()' call - you can insert that into a while loop (while !stream.eof()) and have it do all the work for you, and I have no idea how to input one from the keyboard, my recommendation would be

    "Enter 'end' to end the program: "
    and then do an
    if (s_inputLine.eq ualsIgnoreCase( "end"))
    System.exit()
    structure.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Yes
      Code:
       
      if (s_inputLine.equalsIgnoreCase("end")) 
      System.exit(0)
      Use that.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by r035198x
        Yes
        Code:
         
        if (s_inputLine.equalsIgnoreCase("end")) 
        System.exit(0)
        Use that.
        r0-

        I suppose I should be trying it (and will as soon as I finish this post), but will "end" work? I didn't know that was a Java keyword...

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by sicarie
          r0-

          I suppose I should be trying it (and will as soon as I finish this post), but will "end" work? I didn't know that was a Java keyword...

          end is not a Java keyword. It does not matter in this case anyway because you are getting it as a String literal not using it as a variable

          Comment

          Working...