0

I am trying to read in a string via input from the user and store it in an array. The string must be single digit numbers separated by a space. The user will enter exactly 20 numbers and no number can occur twice in a row.

Example string:

1 2 9 6 3 2 4 5 8 1 3 6 4 7 8 2 1 9 6 3

Here is the code I have to do this, as well as error check and it's not working properly. I think I am way over thinking the error checking. refString is an int array of size 20 in the code below.

case 2:
bool validated = false;
Console.WriteLine("\nPlease enter a 20 character reference string, each separated by a single space");
Console.WriteLine("A number should not occur twice in a row, ex: 1 5 4 4 6");
Console.WriteLine("Each character must be an integer 1-9");
Console.WriteLine("Example reference string:  1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6");
Console.WriteLine("\nEnter reference string:  ");
string s = Console.ReadLine();
refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();  //split string and add numbers into array
int totalCount = refString.Length;

//if the user entered exactly 20 numbers, check to see if any of the numbers occur twice in a row
if (totalCount == 20)
{
    for (i = 1; i < 20; i++)
    {
        while (refString[i] == refString[i - 1])    //make sure two of the same number side by side do not occur
        {
            break;
        }

    }
}

while (totalCount != 20 || !validated)
{
    for (i = 1; i < 20; i++)
    {
        while (refString[i] == refString[i - 1])    //make sure two of the same number side by side do not occur
        {
            Console.WriteLine("\nError: reference string is NOT 20 numbers");
            Console.WriteLine("\nEnter reference string:  ");
            s = Console.ReadLine();
            refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
            totalCount = refString.Length;
        }
    }
    if (totalCount == 20)
    {
        for (i = 1; i < 20; i++)
        {
            while (refString[i] == refString[i - 1])    //make sure two of the same number side by side do not occur
            {
                break;
            }
        }
    }

}

break;
}
0

4 Answers 4

1

Try something like:

case 2:
    bool correctNumber = true;
    bool repeats = false;
    Console.WriteLine("\nPlease enter a 20 character reference string, each seperated by a single space");
    Console.WriteLine("A number should not occur twice in a row, ex: 1 5 4 4 6");
    Console.WriteLine("Each character must be an integer 1-9");
    Console.WriteLine("Example reference string:  1 2 3 4 2 1 5 6 2 1 2 3 7 6 3 2 1 2 3 6");
    Console.WriteLine("\nEnter reference string:  ");
    string s = Console.ReadLine();
    refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();  //split string and add numbers into array
    correctNumber = refString.Length == 20;

    if (correctNumber)
    {
        for (i = 1; i < 20; i++)
        {
            if(refString[i] == refString[i - 1])
            {
                repeats = true;
                break;
            }
        }
    }
    else
    {
        Console.WriteLine("Error: reference string is NOT 20 numbers");
    }
    if (repeats)
    {
        Console.WriteLine("Error: reference string contains repeated numbers");
    }

    break;

That will check for the two fail conditions and output relevant messages.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply. I was able to get it working and most error checking accomplished. The only thing I don't have done for the error handling is protecting against bad input such as if the user enters letters and also if the user enters a two digit number. Would you mind explaining how to do so? isNaN?
0

You can use regex to check if there are two contiguous repeating digits

if(!Regex.IsMatch(n,@".*(\d) \1.*"))
    s.Split(" ").Select(x=>int.Parse(x));

Comments

0
bool correct = false;
while(!correct){
    // ask for numbers until correct
    Console.WriteLine("\nEnter reference string:  ");
    string s = Console.ReadLine();
    refString = s.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
    int totalCount = refString.Length;

    // check    
    if (totalCount == 20) {
        correct = true;
        for (i = 1; i < refString.Length; i++) {
            if(refString[i] == refString[i - 1]) {
                // consecutive number
                Console.WrinteLine("Error: same consecutive number "+refString[i]);
                correct = false;
                break;
            }
        }
    }else{
        Console.WrinteLine("Error: not 20 elements");
    }
}

Comments

0

One trick you can do is use Linq to filter out all the bad values, then check the count hasn't dropped below 20:

var listOfInts = stringTest.Split(' ').Select(n => Convert.ToInt32(n)).ToList();

// filter ints with adjacent values and out of range
var count =
    listOfInts.Where((i, j) => (j + 1 == listOfInts.Count || // adjacent values
                                i != listOfInts[j + 1]) 
                                && i > 0 && i < 10) // out of range
              .Count(); // total

If count < 20, then you know something was invalid (either out-of-range or next to an identical value).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.