0

I am trying to create a sorting system and I am attempting to turn a fairly long list of integers in string form into an int array to more easily be able to sort the array. The initial string array is formed via reading a list of integers from a text file.

This is how the code currently looks and I am currently working on the basis of sorting the year:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;



namespace Climate_Sorting_Application
{
    public class Program
    {
        public static string[] monthArray = File.ReadAllLines("Month.txt");
        public static string[] yearArrayPre = File.ReadAllLines("Year.txt");
        public static string[] afArray = File.ReadAllLines("WS1_AF.txt");
        public static string[] rainArray = File.ReadAllLines("WS1_Rain.txt");
        public static string[] sunArray = File.ReadAllLines("WS1_Sun.txt");
        public static string[] tmaxArray = File.ReadAllLines("WS1_TMax.txt");
        public static string[] tminArray = File.ReadAllLines("WS1_TMin.txt");
        public static string[] af2Array = File.ReadAllLines("WS2_Rain.txt");
        public static string[] rain2Array = File.ReadAllLines("WS2_Rain.txt");
        public static string[] sun2Array = File.ReadAllLines("WS2_Sun.txt");
        public static string[] tmax2Array = File.ReadAllLines("WS2_TMax.txt");
        public static string[] tmin2Array = File.ReadAllLines("WS2_TMin.txt");
        public static string arrayToAnalyse;

        static void Main(string[] args)
        {
            Console.WriteLine("Please Specify the Data to be Analysed");
            Console.WriteLine("You must specify the Text File name (Do not include .txt");
            arrayToAnalyse = Console.ReadLine();



            Console.ReadLine();
        }

        private static void sortProcess()
        {

        }
    }
}

Is there any way of easily converting to the correct data type? Or even a way of converting it to an int value array during the initial file reading?

2 Answers 2

5

Sure! LINQ can really make things simple:

int[] someArray = File.ReadAllLines(filename).Select(int.Parse).ToArray();

That will run every line that's read in through the int.Parse() method and then convert those results to an array.

Sign up to request clarification or add additional context in comments.

Comments

0

ToCharArray? A Char is simply an int in memory.

Or you could read the string in A for loop and do an int.parse(index).

https://msdn.microsoft.com/en-us/library/2c7h58e5(v=vs.110).aspx

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.