The goal of the code is to take a string of numbers separated by hyphens and check if they are consecutive. I have tried using string.spit, iterating over the array and checking if the number 1 is equal to the next value. But I've run into an issue of the index I'm working on exceeding the bounds of the array. Here is the current build.
public static void Consecutive()
{
Console.Write("enter multiple numbers separated by hyphens: ");
var userinput = Console.ReadLine();
var nums = userinput.Split();
int index = 0;
bool isConsecutive = false;
foreach(var number in nums)
{
if(Convert.ToInt32(nums[index 1]) == Convert.ToInt32(nums[index] 1))
{
isConsecutive = true;
}
else
{
isConsecutive = false;
}
if(index == nums.Length)
{
break;
}
}
Console.WriteLine(isConsecutive);
}
CodePudding user response:
This is because when the foreach
loop iterates over the last element there is no element with index 1
and your check if(index == nums.Length)
does not work.
Explanation:
nums.Length = last index 1.
For an array with 5 elements, the length = 5 and the last index = 4.
Because of that your if condition is never true
.
Solution:
Change the condition from if(index == nums.Length)
to if(index == nums.Length - 1)
CodePudding user response:
You want to split by hyphens, but you split by white-spaces. You also don't use the number
loop variable of the foreach
. For what you are doing a for
-loop is much more appropriate anyway:
public static void Consecutive()
{
Console.WriteLine("enter multiple numbers separated by hyphens: ");
string userinput = Console.ReadLine();
string[] nums = userinput.Split('-');
// always validate input
while (!nums.All(n => n.All(char.IsDigit)))
{
Console.WriteLine("All numbers must be valid integers");
userinput = Console.ReadLine();
nums = userinput.Split('-');
}
bool isConsecutive = true;
for(int i = 1; i < nums.Length; i )
{
int thisInt = int.Parse(nums[i]);
int prevInt = int.Parse(nums[i - 1]);
if(thisInt != prevInt 1)
{
isConsecutive = false;
break;
}
}
Console.WriteLine(isConsecutive);
}
CodePudding user response:
Here's a different approach, generating the expected sequence and seeing if that matches the cleansed input:
var delimiter = ",";
Console.Write("Enter multiple integers separated by '" delimiter "': ");
var userinput = Console.ReadLine(); ;
bool isConsecutive = false;
try
{
var strNums = userinput.Split(delimiter.ToCharArray());
var nums = Array.ConvertAll(strNums, x => int.Parse(x));
var consecutive = Enumerable.Range(nums[0], nums.Length);
var strCleansed = String.Join(delimiter, nums);
var strGenerated = String.Join(delimiter, consecutive);
isConsecutive = (strCleansed == strGenerated);
Console.WriteLine(" cleansed = " strCleansed);
Console.WriteLine(" generated = " strGenerated);
}
catch (Exception)
{
Console.WriteLine("Invalid Integer in Input");
}
Console.WriteLine("isConsecutive = " isConsecutive);
Console.ReadKey();
Sample runs:
Enter multiple integers separated by ',': -3, -0000002, -1, 0, 1, 02, 3
cleansed = -3,-2,-1,0,1,2,3
generated = -3,-2,-1,0,1,2,3
isConsecutive = True
Enter multiple integers separated by ',': 1,2,3,5
cleansed = 1,2,3,5
generated = 1,2,3,4
isConsecutive = False
Enter multiple integers separated by ',': 1,fish,2,fish,red fish,blue fish
Invalid Integer in Input
isConsecutive = False