Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test to ensure the user input is a double and is greater than zero?

In C# I am trying to have the user input a number. I then want to check that

  1. They have entered a string that can be converted to a double and
  2. They have entered a value greater than zero

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.

like image 829
firepower4 Avatar asked May 28 '16 16:05

firepower4


People also ask

How do you test if an input is a double in Java?

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.

How do you check if a number is greater than 0 in Python?

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.

How do you check if a input is greater than Python?

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.


1 Answers

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.

like image 162
BJ Myers Avatar answered Nov 15 '22 05:11

BJ Myers