0

i am new in programming and for now only practicing with C#. So my problem is: i am trying to separate a number in a digits with array (example: number 12345 in to digits {1,2,3,4,5}). I make some code, here is it:

  int num = int.Parse(Console.ReadLine());
        int[] digits = new int[3];
        int separatedDigit = 0;


        for (int i = num; num != 0; i--)
        {
            digits[i] = num  % 10;
            num = num / 10;

        }

but it shows me error " Index was outside the bounds of the array." I suppose the problem is coming from that "for" part because it starts from position 3 and the array have only 2 (0, 1, 2). I don't know how to fix it, so can someone help me?

4
  • Position #3, is actually referenced as [2], since you start at position [0] Commented Dec 16, 2016 at 20:28
  • when your for loop starts, you are trying to do like digits[12345] = 5, which .net runtime woudn't allow as index '12345' doesn't exist in the array. Commented Dec 16, 2016 at 20:30
  • 12345 is just example in my situation the number is allays 3 digits long... Commented Dec 16, 2016 at 20:33
  • how to make it to start form position 0 because i tried for (int i = 0; num != 0; i++) { digits[i] = num % 10; num = num / 10; } but still didn't work Commented Dec 16, 2016 at 20:37

5 Answers 5

1

Try Linq: filter out '0'..'9' characters and materialize them into array:

int[] digits = Console
  .ReadLine()
  .Where(c => c >= '0' && c <= '9') // '0'..'9' characters only 
  .Select(c => c - '0')             // '0' should correspond to 0 integer
  .ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

While this does the job, I feel like it doesn't address OP's core question. Keep in mind that OP is a beginner, and wants to know not just "what" but also "why".
0

i starts out as equal to num, which in turn starts out as the number that you entered, which can be far greater than 3. For example, if I put in 123 as the input number, then the loop first tries to access digits[123] which is waaaaaay outside the bounds of that array.

You're going to want to tweak your for loop to get i to start at a more reasonable number:

for (int i = digits.Length - 1; num != 0; i--)
{
    // ...

Alternatively, you could start i at 0 and work your way up:

for (int i = 0; num != 0; i++)
{
    // ...

Comments

0

Try this

        int _num = 12345;
        var g = _num.ToString().Select(x => int.Parse(x.ToString())).ToArray<int>();

Comments

0

Try this

 string strValue = "19345abc#/";
 char[] charArray = strValue.ToCharArray();
 List<int> list = new List<int>();
 for (int i = 0; i < charArray.Length; i++)
     {
       if (char.IsNumber(charArray[i]))
         {
           list.Add(charArray[i] - '0');
         }
     }

Comments

0

The problem is that you haven't allocated the correct number of spaces when initialising the array, for example in the following line of code: int[] digits = new int[3];.. Instead you should first read the input, calculate the number of numbers (characters) that were input and then allocate an array with that number of elements.. Remember that arrays start at the index of 0.. so in your example the first character (number) would be entered at digits[0].. then digits[1]... digit[2].. etc.. from within the loop until all the numbers have been input.

Try this code:

    string numberSequence = Console.ReadLine();
    var numberCount = numberSequence.Length;
    int[] digitArray = new int[numberCount];
    int i = 0;
    foreach (var number in numberSequence)
    {
        digitArray[i] = number;
        Console.WriteLine(number);
        i++;
    }
    Console.ReadLine();

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.