Question

I need to put some numbers to an array from console input, where the first number decides how many there will be (that number *2). The numbers have at most 2 digits. My code so far, somehow still not working.

        int quantity = Convert.ToInt32(Console.Read());          

        int[] arr = new int[100];               
        int count = 0;

        while (count < quantity*2)             
        {
            string line = Console.ReadLine();
            line = line + ' ';

            for (int i = 0; i < line.Length; i++)
            {
                char c = line[i];
                if ((c>='0')&&(c<='9'))
                {
                    char d = line[i + 1];
                    if ((d >= '0') && (d <= '9'))
                    {
                        arr[count] = c * 10 + d;
                        i++;
                    }
                    else 
                    {
                        arr[count] = c;
                    }
                    count++;

                }
            }
Was it helpful?

Solution

There are a couple of errors that cannot allow your program to work correctly.

// Do not use Console.Read, but ReadLine to wait the end of input from your user
string userInput Console.ReadLine();          

// Convert to a number using Int32.TryParse to check if your user really types a number
// and not something that will raise an exception if it is not a number
int quantity;
if(!Int32.TryParse(userInput, out quantity))
{
    Console.WriteLine("An integer number is required!");
    return;
}

// Dimension you array size large enough to contain the numbers required in the
// following loop (I am not sure if now you require to double the size of the array)
int[] arr = new int[quantity*2];               
int count = 0;

while (count < quantity*2)             
{
    string line = Console.ReadLine();
    int inputNumber;

    // Again to convert to a number, use Int32.TryParse 
    if(Int32.TryParse(line, inputNumber))
    {
        arr[count] = inputNumber;
        count++;
    }
    else
    {
        Console.WriteLine("An integer number is required!");
    }
 }

Using Int32.TryParse allows a better handling of user inputs. No need to work with the individual chars searching for invalid input. (By the way, your actual method limits your code to numbers not bigger than two digits)

However, in context like this, when you don't know the exact size of the array, the recommended data structure to use is List<int>

// Create a List of integers, the size is not needed
List<int> arr = new List<int>();
int count = 0;

// Not sure if now you require to double the size of the array
while (count < quantity)             
{
    string line = Console.ReadLine();
    int inputNumber;

    if(Int32.TryParse(line, inputNumber))
    {
        // Add the input to the list....
        arr.Add(inputNumber);
        count++;
    }
    else
    {
        Console.WriteLine("An integer number is required!");
    }
 }

You could easily work with a List as it was an array

 for(int x = 0; x < arr.Count; x++)
     Console.WriteLine(arr[x].ToString());

EDIT Following your comment below. If the line could contain multiple numbers then you need the split on the user input, but this doesn't impact the TryParse check, you just need an additional loop and a control on the maximum count of elements inserted

int[] arr = new int[quantity];               
int count = 0;
while (count < quantity)             
{
    string line = Console.ReadLine();
    int inputNumber;

    // Split on space and tabs and remove the resulting 
    // empty elements if there are two space/tabs consecutive.
    string[] parts = line.Split(new char[] {' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
    foreach(string aNumber in parts)
    {
         if(Int32.TryParse(line, inputNumber))
         {
             arr[count] = inputNumber;
             count++;
         }
         else
         {
              Console.WriteLine(aNumber.ToString() + " is not an integer number!");
         }
         if(count >= arr.Length)
             break;
    }
}

OTHER TIPS

I think this works:

Console.Write("Enter the number to collect (*2):");
int initialQuantity = Convert.ToInt32(Console.ReadLine());
int calculatedQuantity = initialQuantity * 2;
int[] arr = new int[calculatedQuantity];
int count = 0;

while (count < calculatedQuantity)
{
    Console.Write("Enter the number:");
    string line = Console.ReadLine();
    int inputNumber;
    if (int.TryParse(line, out inputNumber) && line.Length <= 2)//is valid
        arr[count++] = inputNumber;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top