2

I need to check a set of user Input from my console application before triggering my method and store data into my database.

The program compiles and rund without exceptions. But in case of one wrong Input it still runs through for the other three.
Although, what I really need is to make sure the 4 user's entries are correct before triggering the method and in case just one is wrong the whole program should stop and exit.

using System;
using System.Threading;

namespace BarcodeValidation
{
    class Program
    {
        static void Main(string[] args)
        {
            ReadBarcode();
        }

        static void ReadBarcode()
        {
            var barcodes = GetInput();

            foreach (var item in barcodes)
            {
                // something
                CheckUserInput(item);
            }
        }

        static string[] GetInput()
        {
            Console.WriteLine("Please enter 4 products ID, Barcodes, MPN or EAN code:");

            string[] barcode = new string[4];

            for (int i = 0; i < barcode.Length; i++)
            {
                barcode[i] = Console.ReadLine();
            }
            return barcode;
        } // end of method here

        static void CheckUserInput(string userInput)
        {
            int msec = 5000;

            try
            {
                if (!(userInput == "F5121" || userInput == "F3111" || userInput == "F8331" || userInput == "F5321"))
                {
                    Console.WriteLine("Enter a valid MPN codes for your products");
                    Thread.Sleep(msec);
                    Environment.Exit(0);
                }
                else
                {
                    switch (userInput)
                    {
                        case "F5121":
                            Console.WriteLine("barcode 1 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F3111":
                            Console.WriteLine("barcode 2 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F8331":
                            Console.WriteLine("barcode 3 is =", userInput);
                            Thread.Sleep(msec);
                            break;
                        case "F5321":
                            Console.WriteLine("barcode 4 is =", userInput);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}     
6
  • Isn't that what you are doing? for each item, check user input. CheckUserInput then exits if the input is incorrect. What in your current program isn't working? Commented May 3, 2017 at 13:42
  • bool errorOccurred = false; => replace foreach w/ while(errorOccurred == false) and set errorOccurred to true once some input is invalid ? Check is again after your look to see if continue or not . Commented May 3, 2017 at 13:43
  • What part of your program do you want to "not run" ? because it would exit the program if at least 1 of your entries is incorrect, you already have that right. It will exit immediately. however if all 4 pass "CheckUserInput" that's the end of your program already as that's all that happens in "ReadBarcode"... what do you want to not run if any of them are wrong? Commented May 3, 2017 at 13:46
  • I don't want to get in this specific case the console Output from the Switch Statement. Commented May 3, 2017 at 14:00
  • @JNW have you tried my answer ? Commented May 3, 2017 at 14:05

3 Answers 3

2

Since you have a method that actually tests your user input use it's return value:

static bool CheckUserInput(string userInput) // true : valid | false : invalid
{
    int msec = 5000;
    try
    {
        if (!(userInput == "F5121" || 
              userInput == "F3111" || 
              userInput == "F8331" || 
              userInput == "F5321"))
        {     
            Console.WriteLine("Enter a valid MPN codes for your products");
            return false;
        }
        else
        {
            switch (userInput)
            {
                case "F5121":
                    Console.WriteLine("barcode 1 is =", userInput);
                    Thread.Sleep(msec);
                    return true;                      
                case "F3111":
                    Console.WriteLine("barcode 2 is =", userInput);
                    Thread.Sleep(msec);
                    return true;
                case "F8331":
                    Console.WriteLine("barcode 3 is =", userInput);
                    Thread.Sleep(msec);
                    return true;
                case "F5321":
                    Console.WriteLine("barcode 4 is =", userInput);
                    return true;
                default:
                    return false;
            }
        }

    }
    catch (Exception ex)
    {

        Console.WriteLine(ex.Message);
        return false;
    }

} 

ReadBarcodes could look like this:

static void ReadBarcode()
{
    var barcodes = GetInput();
    bool errorOccured = false;
    foreach (var item in barcodes)
    {
        // something
        if(!CheckUserInput(item))
        {
            errorOccured = true; // keep track of that error
            break; //Break for if 1 input is invalid
        }
    }
    //Further execution....
    if(errorOccured)
    {
        return; //Do not continue ...
    }
    //Do other things you want to do. Your input is valid at this point !
}

or shorter like Default quoted:

static void ReadBarcode()
{        
    if(!GetInput().All(CheckUserInput))
    {
        return;
    }
    //Your stuff goes here. Input is valid at this point
}
Sign up to request clarification or add additional context in comments.

4 Comments

or use LINQ: bool success = barcodes.All(CheckUserInput);
May also be useful to include the "default" value in the switch, and move the if check code into the default, which is implied if it matches none of the cases.
I Need to program to first check if the 4 Inputs are valid Inputs (Barcode or MPN) before it gives the any of the console Output from the Switch Statement. i have tried the above but still gets to console Output if just one out the 4 is valid.
I got the missing element from your answer @Felix . thanks that works too.
0

One option you could employ is create your own class that derives from System.Exception, and in cases where one of the inputs is found to be invalid, you could throw an instance of your exception class.

You could wrap your code in a try-catch block, and then put the remediation code within the catch block.

Comments

0

You need to break the checking code and the "output" code into different places. You need to check if all the values are valid values. and AFTER you have checked all the values, then do your console.writelines (Which is the part you dont want to happen). At the moment it checks one and executes the code if that one is valid, and then moves on to the next one. CheckUserInput should really ONLY check the users input, it should not do something else you want to restrict based on that methods result. You should have CheckUserInput and a ExecuteBarcodeStuff for example, and only if all CheckUserInputs return true, should you run the new (as yet unimplemented) ExecuteBarcodeStuff

Mixing this approach with other peoples answers that do Linq queries or such to ensure all the results were positive matches will get you the result you desire.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.