Reputation: 1647
I am building an application that takes two inputs from user. One int and the other float.
int.MaxValue
and float.MaxValue
are the two properties I am using to validate the input.
However, the input is built in such a way by adding each KeyChar
because each key input has to be validated during Key press event as application restricts users to number (whole/decimal).
Here the code so far:
ConsoleKeyInfo key;
const int intMaxValue = int.MaxValue;
const float floatMaxValue = float.MaxValue;
bool validated;
int tempAmount;
int intAmount;
string tempVal="";
do{
key = Console.ReadKey (true);
if (key.Key != ConsoleKey.Backspace)
{
validated = int.TryParse (key.KeyChar.ToString(), out tempAmount);
if (validated)
{
///Full amount is built here
tempVal += key.KeyChar;
//Fails here--->> as Type Cast happens on a String which hold a value beyond an int
if (int.Parse(tempVal) < intMaxValue)
{
intAmount = int.Parse(tempVal);
Console.Write(key.KeyChar);
}
I could do this test after user has pressed Enter Key. However still it will be failed. Becuase there's no way an int could hold beyond its max Value..
Thus I came up with the following: However it doesn't feel efficient as my second input validation is based on float
....
if (float.Parse(tempVal) < intMaxValue)
{
Therefore someone please show me an efficient property of int, float class that can be used in the two scenarios.. OR any other way to sew the input while validating each key digit by digit until user enters Enter key.
Upvotes: 0
Views: 2028
Reputation: 63387
You can try this:
if (validated)
{
///Full amount is built here
long l = long.Parse(tempVal + key.KeyChar);
if (l < intMaxValue)
{
intAmount = (int)l;
tempVal += key.KeyChar;
Console.Write(key.KeyChar);
}
However, as others suggested, you should use Console.ReadLine
and use TryParse
in combination with a while
loop until the input is validated
.
After some deeper scan into your code, I think yo don't need the tempVal
when you already have intAmount
, just do something like this:
if (validated)
{
long k = intAmount*10L + (int) key.KeyChar - 48;
if (k < intMaxValue)
{
intAmount = (int)k;
Console.Write(key.KeyChar);
}
Upvotes: 1
Reputation: 216363
Working with ReadLine will be easier
bool validated = false;
int integerRequired;
Console.WriteLine("Type an integer number:");
while(validated == false)
{
string userInput = Console.ReadLine();
validated = int.TryParse (userInput, out integerRequired);
if (!validated )
Console.WriteLine("Not a valid integer, please retype a valid integer number");
}
float singleRequired;
validated = false;
Console.WriteLine("Type a floating point number:");
while(validated == false)
{
string userInput = Console.ReadLine();
validated = Single.TryParse (userInput, out singleRequired);
if (!validated)
Console.WriteLine("Not a valid float, please retype a valid float number");
}
In this example, you react to the user input only when he finishes to type and press enter. At this point you could evaluate the input using the version of TryParse appropriate for the input and choose to display or not an error message to the user.
Upvotes: 0
Reputation: 17680
You could try using TryParse
.
TryParse
will evaluate to false if the value isn't a valid Int32
(if will be false if it exceeds Int32.MaxValue
).
int realValue;
if (Int32.TryParse(tempVal, out realValue))
{
intAmount = int.Parse(tempVal);
Console.Write(key.KeyChar);
}
You can also use the correspondingfloat.TryParse
to test the float values you expect.
Upvotes: 1