10

I am trying to read some values in c# from console and then processing them. However i am stuck at a error.

The input from console is:

Name:ABCD
School:Xyz
Marks:80
 //here the user enters a new line before entering new data again
Name:AB
School:Xyz
Marks:90
//new line again 
Name:AB
School:Xyz
Marks:90

and so on. I do not know before hand the number of console inputs...How can i detect that user has stopped entering & store the input.

I tried using

string line;
while((line=Console.ReadLine())!=null)
{
  //but here it seems to be an infinite loop 
}

Any suggestions

5 Answers 5

12

Your code looks for "end of console input" which is "Ctrl+Z" as covered in Console.ReadLine:

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop. The following example illustrates this scenario.

If you are looking for empty string as completion use String.IsNullOrWhiteSpace.

string line;
while(!String.IsNullOrWhiteSpace(line=Console.ReadLine()))
{
  //but here it seems to be an infinite loop 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note: You have to hit the return button after ctrl+z for it to finish the ReadLine command
2

There's no way for you to know implicitly that the user has finished entering all their data. You would need some explicit entry from the user to tell you that no more data was coming.

1 Comment

Note that "ReadLine will never return null" is not exactly true - check MSDN link in my answer.
2

Line will never be null, that's why it's an infinite loop. You have to check for empty string or specific value like the letter q (for quit). I recommend you the following.

string line;
do
{
    //input code

    //Check for exit conditions
    line = Console.ReadLine();
} while (!String.IsNullOrWhiteSpace(line) || line.ToLower() != "q");

Comments

0

this is more easier isn't it Example: i am adding inputs of each line to a list

        List<int> inA = new List<int>();
        var inp = Console.ReadLine();
        while (inp != string.Empty)
        {
            inA.Add(Convert.ToInt32(inp));
            inp = Console.ReadLine();

        }

1 Comment

This already has an answer that checks for both null and whitespace. From two years ago. Also, the OP just wanted to properly terminate the loop; adding elements to an array is just adding unneeded code superfluous to the problem.
0

You could do this

var input = Console.In.ReadToEnd();

Caveats

  1. You will get the full input at once not line by line
  2. User will have to indicate the end of input using EOF markers, which on windows systems is ^z (i.e. Ctrl+z)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.