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;
}