0

I have this question from a test paper that im having trouble with:

Write the body of code for the method

public static int enter_No_Of_Items(int min, int max)

which will allow the user to enter a value for the number of items bought.

The value should be validated in the range min to max. Your answer should include data declaration, prompt and input statements, range check, error message control.

It might just be me but the question seems silly because I would think that validation would return true or false rather than an integer, if someone could help me answer this or explain exactly what the question wants that would be great, any help is appreciated.

5
  • 1
    Only the author of the question can answer what is expected, however in the C days (before there was a boolean data type) 0 was used for false and anything non-zero was used for true
    – D Stanley
    May 1, 2015 at 14:15
  • 5
    please tell me your instructor is not teaching you to create method names by using underscores between words
    – Rufus L
    May 1, 2015 at 14:21
  • 3
    @RufusL: Indeed. Or else we will teach him a lesson ;) May 1, 2015 at 14:22
  • 1
    Does not make sense to me either. It does not even pass in the integer to be validated. If this is WPF there is some really good validation tools.
    – paparazzo
    May 1, 2015 at 14:22
  • 2
    Two other things - public methods are typically PascalCase, and they should describe what the method does. This one appears to be named with the prompt that would be given to the user rather than what it actually does. A better name might be GetItemCount.
    – Rufus L
    May 1, 2015 at 14:29

2 Answers 2

2

You are right. Generally a validation:

  • returns a bool indicating good or wrong;
  • doesn't have a return value and throws an exception if invalid.

However, in this case, you are asked to 'ask the user' to enter a value. The scope of your method isn't only validation, it is retrieving input too. That is probably the return value expected.

You can either throw an exception if the number entered is invalid, or just keep asking until they finally enter a valid number.

4
  • so if the users input is between the min and max the value is returned? May 1, 2015 at 14:09
  • 3
    To extend this (since it is spot on), since the assignment doesn't specify use only one method, why not extract your validation method (that can return boolean) into another method and call it from No_Of_Items? May 1, 2015 at 14:12
  • Because "Number of items" is a positive number (including zero) in this world, you could return -1 for "invalid" as another solution.
    – DrKoch
    May 1, 2015 at 14:33
  • 2
    @DrKoch: True, but that is very dependent on the actual requirements. If you use an int, you should expect the entire range, not just the positive subset. May 1, 2015 at 15:06
0

I had a go at writing the method and this is the best I could come up with:

public static int enter_No_Of_Items(int min, int max)
    {
        string input = "input";
        bool ok = false;
        while (ok == false)
        {
            Console.WriteLine("Enter a number between {0} and {1}.", min, max);
            input = Console.ReadLine();

            if ((int.Parse(input) >= min) && (int.Parse(input) <= max))
            {
                Console.WriteLine("Valid number.");
                break;
            }

            Console.WriteLine("Invalid number.");
        }

        return int.Parse(input);
    }

    static void Main(string[] args)
    {
        int randomA = 5;
        int randomB = 9;

        int itemCount = enter_No_Of_Items(randomA, randomB);
        Console.ReadLine();
    }

If i could get feedback on how to improve it, that would be great. Thanks.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.