Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:27:57+00:00 2026-06-17T13:27:57+00:00

I am writing a basic Scanner class similar to Java’s. Here is what I

  • 0

I am writing a basic Scanner class similar to Java’s. Here is what I have (well, the relevant parts):

using System.Text;
using System.Collections.Generic;

namespace System.IO
{
    /// <summary>
    ///     <remarks>
    ///         Scanner is a wrapper for a <see cref="System.IO.TextReader" />
    ///         instance, making it easier to read values of certain types. It
    ///         also takes advantage of the
    ///         <see cref="System.IO.EndOfStreamException" /> class.
    ///     </remarks>
    ///     <seealso cref="System.IO.TextReader" />
    /// </summary>
    public class Scanner
    {
        private TextReader Reader;
        private Queue<char> CharacterBuffer = new Queue<char>();

        /// <summary>
        ///     <remarks>
        ///         Defaults to reading from <see cref="Console.In"/>
        ///     </remarks>
        /// </summary>
        public Scanner() : this(Console.In)
        {
        }

        public Scanner(TextReader reader)
        {
            this.Reader = reader;
        }

        public char Peek()
        {
            if (this.CharacterBuffer.Count > 0)
                return this.CharacterBuffer.Peek();

            try
            {
                return Convert.ToChar(this.Reader.Peek());
            }
            catch (OverflowException)
            {
                throw new EndOfStreamException();
            }
        }

        public char ReadChar()
        {
            if (this.CharacterBuffer.Count > 0)
                return this.CharacterBuffer.Dequeue();

            try
            {
                return Convert.ToChar(this.Reader.Read());
            }
            catch (OverflowException)
            {
                throw new EndOfStreamException();
            }
        }
    }
}

From the few tests that I ran, this works great with real files, but it doesn’t quite work as expected with stdin. If I use Scanner.Peek or Scanner.ReadChar, after a newline is sent, the TextReader instance thinks it is at the end of the file (I think) and throws an EndOfStreamException instance since this.Reader.Read and this.Reader.Peek returns -1.

How can I force it to request a new character while still supporting true files and StringReader instances?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:27:59+00:00Added an answer on June 17, 2026 at 1:27 pm

    I changed Peek to reflect the following code. I’m not sure if this is 100% because I typed this from memory directly here.

    public char Peek()
    {
        if (this.CharacterBuffer.Count > 0)
            return this.CharacterBuffer.Peek();
    
        char Character
        try
        {
            Character = Convert.ToChar(this.Reader.Read());
        }
        catch (OverflowException)
        {
            throw new EndOfStreamException();
        }
    
        this.CharacterBuffer.Enqueue(Character);
        return Character;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a basic tic tac toe game in Java. In my Board class
I want to get some random text generated. I tried writing a basic Java
I'm writing a basic text editor, well it's really an edit control box where
I am writing a basic game engine and have an abstract class that represents
I'm writing very basic schema-based Spring AOP, here's the .xml <bean id=aoplistener class=tao.zhang.Listener/> <aop:config>
I'm writing a basic push messaging system. A small change has caused it to
I'm writing a basic war-driving program. I have gotten it to loop the command
I am writing a basic Java application with a Swing front-end. Basically it loads
I am writing a basic wizard for my web site. It will have 4
I am writing a basic toon shader in OpenGL. I am using MSVC 2008.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.