In C# I am trying to have the user input a number. I then want to check that
The method I have initially created was
string inValue;
double outcome;
Console.WriteLine("Enter amount: ");
inValue = Console.ReadLine();
while (double.TryParse(inValue, out outcome) == false)
{
Console.WriteLine("Initial value must be of the type double");
Console.WriteLine("\nPlease enter the number again: ");
inValue = Console.ReadLine();
}
outcome = double.Parse(inValue);
while (outcome < 0)
{
Console.WriteLine("Initial value must be of at least a value of zero");
Console.WriteLine("\nPlease enter the number again: ");
inValue = Console.ReadLine();
outcome = double.Parse(inValue);
}
return outcome;
The problem was this was that if the user entered say "-10" and then "f" an exception would occur. This is because the program would move past the first check (that checks for the double) for the value of -10 but then when the "f" is entered it throws an exception when only given the second test.
I believe the solution is to create a while statement that writes the error statement when either the value cannot be converted to a double or the value is converted to a double and is below zero. What I don't know how to do is to have the value be converted to a double and then evaluated as being greater than zero all in the while statement.
The hasNextDouble() is a method of Java Scanner class which is used to check if the next token in this scanner's input can be interpreted as a double value using the nextDouble() method. It returns true if the scanner's input can be interpreted as a double value, otherwise returns false.
To verify if a user input number is greater than 0: Use a while loop to iterate until the user provides a positive number. On each iteration, check if the user provided a number greater than 0. If both conditions are met, break out of the while loop.
The Python greater than or equal to >= operator can be used in an if statement as an expression to determine whether to execute the if branch or not. For example, the if condition x>=3 checks if the value of variable x is greater than or equal to 3, and if so, enters the if branch.
You're on the right track - you need to have a single while loop that gets the input and then tries both validations. One way to do this is to create a boolean value that tracks whether or not the value is valid, and then use that as the condition for the loop:
double outcome = 0;
bool valid = false;
while (!valid)
{
Console.WriteLine("Enter amount: ");
string inValue = Console.ReadLine();
if (double.TryParse(inValue, out outcome) == false)
{
Console.WriteLine("Initial value must be of the type double");
Console.WriteLine("\nPlease enter the number again: ");
}
else if (outcome < 0)
{
Console.WriteLine("Initial value must be of at least a value of zero");
Console.WriteLine("\nPlease enter the number again: ");
}
else
{
valid = true;
}
}
return outcome;
It's also possible to put both conditions in the while statement, but this approach lets you provide a different message depending on which conditions failed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With