I have hard-coded this array of ints

int[] numbersToSort = {100, 23, -1, 3, 99, 0};

I want to use Console.Readline() to take input from the user, but it only takes string input. I can't use Convert.ToInt32() because it doesn't take array for parameter.
How can i convert array of string to array of int?

Recommended Answers

All 2 Replies

You want that user write number by number (one per each time) and the app. should create an array?

Is this what you had in mind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Feb18IntArray_ReadLine
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = null;
            int counter = 1;
            Console.WriteLine("Please write numbers (one number each time):");
            Console.WriteLine("(If you want to see all the numbers inserted, press q (to quit))");

            while (true)
            {
                string item = Console.ReadLine();
                if (item != "q")
                {
                    int value = 0;
                    bool bChecking = int.TryParse(item, out value);
                    if (bChecking)
                    {
                        Array.Resize(ref array, counter);
                        array[counter - 1] = value;
                        counter++;
                        Console.WriteLine("Good, next one...");
                    }
                    else
                        Console.WriteLine("This was not a number, please do it again...");
                }
                else
                    break;
            }
            string values = null;
            for (int i = 0; i < array.Length; i++)
                values += array[i].ToString() + " ";
            Console.WriteLine("This are all the values: {0}", values);
            Console.ReadLine();
        }
    }
}
commented: Clear code +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.