I'm new to C# and I'm having some trouble reading some ints out of a Console Window App. What I need to do is have a user enter in some integers and press spacebar and enter more numbers and I need to evaluate the numbers one at a time on the fly without the user pressing Enter. I then have to do so other stuff with the numbers but that's not an issue. Should I use Console.Read() or Console.ReadKey(), I know ReadLine() wont do anything until enter is pressed so wont do what I want.
3 Answers
For your case, ReadKey is more suitable than Read() since the Read terminates when you press the Enter key. But ReadKey() is like below:
The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. However, pressing a modifier key by itself will not cause the ReadKey method to return.

- 1,090
- 10
- 19
Hope This will be helpful for you Difference between Console.Read() and Console.ReadLine()?
As I understand your question you can use Console.ReadLine() instead of using Console.Read()...Give me further details to update more...

- 1
- 1

- 11
- 2
-
I think ReadLine should avoid for this case since user3488019 wants to process key input on the fly. – Infinity Challenger Aug 13 '15 at 03:26
-
I need to be able to enter digits i.e - 1 2 5 6 8 (or what ever digits are entered, the digits aren't pre-defined so are whatever are entered on the Console) but they need to register while they are being entered so I can run a test on the digits that are being entered as they are being entered. Does this make sense. – user3488019 Aug 13 '15 at 04:30
-
You can use comma separated/space separated integers as input and then spilt and use according to your requirement, that means after entered all of your integers then press enter key, use Console.ReadLine() for this...:) – Nadarajah Pragash Aug 13 '15 at 05:42
-
Try this code, It may help you Console.WriteLine("Please enter your integer numbers :"); var numbers = Console.ReadLine(); string[] splited = numbers.Split(','); foreach(var num in splited) { Console.WriteLine("Splited Integer : {0}",Convert.ToInt32(num)); } Console.ReadLine(); – Nadarajah Pragash Aug 13 '15 at 05:53
-
http://goo.gl/x2KtSl... Please go to this link and check how it's working...As I guess it will be enough to cater your input parameters – Nadarajah Pragash Aug 13 '15 at 06:09
-
Sorry I don't think I'm being clear the evaluation has to be done on the fly not after enter is pressed.. So the first number is typed into the console, an if statement does a test on the number then another number is put in the console and a test is done this will repeat until the desired outcome and only then will enter be pressed. Thanks in advance for your work.. – user3488019 Aug 13 '15 at 08:11
I ended up using a ReadKey(), which seemed to work well. Thanks for the help anyway.
numberIntoConsole = Console.ReadKey();
numberReceived = (int)Char.GetNumericValue(numberIntoConsole.KeyChar);
if (numberReceived != -1) //checks to see if a spacebar was pressed
{
if(numberReceived == lastNumberEntered) //checks if numberReceived is equal to lastNumberEntered
{
lastNumberEntered = numberReceived; //make the lastNumberEntered the same as numberReceived
++numberChecker; // add to the numberChecker
} else // else we restart the counter
{
numberChecker = 1;
lastNumberEntered = numberReceived;
}
}

- 51
- 8