Why is Console.Readline not executing as expected

你说的曾经没有我的故事 提交于 2019-11-30 15:55:42

The problem is the use of the problematic method Console.Read(). This method will block until a full line of text is entered, but it only returns the first character, leaving the remaining text in the input buffer. The carriage return that was used to enter the first value and unblock the read operation is still sitting in the input buffer when the Console.ReadLine() operation is performed. Consequently, a blank line is returned.

I recommend that you replace Console.Read().ToString() with Console.ReadLine() to fix this problem.

The problem can be demonstrated with this code:

    static void Main(string[] args)
    {
        string value = Console.Read().ToString();
        Console.WriteLine("You entered: {0}", value);
        Console.WriteLine("Press ENTER to continue...");
        Console.ReadLine();   // Returns immediately.
        Console.WriteLine("Continuing....");
    }

And fixed like this:

        string value = Console.ReadLine();

Is your project configured as a console application? It needs to be built with different switches in order to set the flags in the exe file so that the OS loader will know that it needs to create a console window for the process. GUI apps don't set that flag, and don't get a console window.

In VS2010, right click on your project's Properties link, click on the Application tab, and change Output Type from Windows Application to Console Application. Rebuild and run.

You probably have duplicate / extra newlines in your input. This code looks perfectly fine for parsing a comma-delimited file...

The issue could be stuff already in the input stream you're trying to read from the console. If you don't want to worry about what's already in the buffer, you can try to flush it so that you start with a fresh, empty input state. Offhand, all the Console.Read* functions appear to be blocking functions. However, there's also the KeyAvailable property which indicates if there is content available.

This provides for the following code:

private void clearInputBuffer()
{
   while(Console.KeyAvailable)
   {
       Console.Read(); // read next key, but discard
   }
}

Alternatively, the buffer for input is, at the basic level, a stream. A TextReader, to be specific. And that is available from

Console.In

So you can use the functions there, such as .ReadToEnd(), to clear out the buffer right before you enter in your While(true) loop.

The problem is that you're misinterpreting what Console.Read does. It reads the next character from the input stream and returns it as an integer. But ... and here's the good part, nothing comes in until you hit Enter. So if you enter "abc" at the first prompt and press Enter, you're going to get the first character from the buffer, as an integer (97). But the input buffer will contain "bc" and the newline, which will then be consumed by the Readline.

If you want a string from the Console, call ReadLine, not Read.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!