Home > Mobile >  How do I split my text file into an 2 arrays
How do I split my text file into an 2 arrays

Time:09-17

This is my code so far

string filename = @"marks.txt";
try
{
    StreamReader reader = new StreamReader(filename);
    using (reader)
    {
        int lineNum = 0;
        string line = reader.ReadLine();
        while (line != null)
        {
            lineNum  ;
            Console.WriteLine("{0}", line);
            line = reader.ReadLine();
        }
        Console.WriteLine(reader.ReadToEnd());
    }
}
catch (FileNotFoundException)
{
    Console.Error.WriteLine("Can not find the file {0}", filename);
}
catch (DirectoryNotFoundException)
{
    Console.Error.WriteLine("Invalid directory in file path.");
}
catch (IOException)
{
    Console.Error.WriteLine("Can not open the file {0}", filename);
}

What I want to make it so it reads the file which has a bunch of marks and names in it and split them into 2 separate arrays so I can print out who got the highest and lowest mark, the average mark and also put everyone who got over a certain mark like 80 and write a new file for them.

edit: The File Roughly looks like this for about 20 lines:

50 Adam
23 Jennifer
85 Sanjay

CodePudding user response:

Let's say your file has marks and names like this:

John Doe, 89
David Smith, 74
Ramkumar Sundararajan, 85

In your code where you read lines, you can split the tokens like this:

string line = reader.ReadLine();
var tokens = line.Split(',');
string name = tokens[0];
int marks = int.Parse(tokens[1]);

You will need to handle errors if the input is not in the right format.

Once you have the name and the marks, you can store them in a Dictionary or in two different arrays and operate on the values.

CodePudding user response:

Here you go:

string[] lines = File.ReadAllLines(@"file.txt");

var scores =
    lines
        .Select(x => x.Split(new[] { ' ' }, 2))
        .Select(x => new { name = x[1], score = int.Parse(x[0]) })
        .ToArray();

string highest = scores.OrderByDescending(x => x.score).Select(x => x.name).First();
string lowest = scores.OrderBy(x => x.score).Select(x => x.name).First();
double average = scores.Select(x => x.score).Average();

var over80 = scores.Where(x => x.score > 80).ToArray();
File.WriteAllLines(@"output.txt", over80.Select(x => $"{x.score} {x.name}"));

Now, in case you have two or more people with the highest or the lowest score, then you need this:

string highest = String.Join(", ", scores.ToLookup(x => x.score, x => x.name)[scores.Max(x => x.score)]);
string lowest = String.Join(", ", scores.ToLookup(x => x.score, x => x.name)[scores.Min(x => x.score)]);

CodePudding user response:

Lets assume your file contains data like below ABC:91|BCD:92|EDR:93 You can first split file by "|" sin and then with ":" sign and populate 2 arrays like this

 string fileName = @"marks.txt";
 //string strData = "ABC:91|BCD:92|EDR:93";
            StreamReader sr = new StreamReader(fileName);
            string strData = sr.ReadToEnd();
            sr.Close();
            ArrayList alName = new ArrayList();
            ArrayList alMarks = new ArrayList();
            string[] strSplit1 = strData.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strSplit1.Length; i  )
            {

                string[] strSplit2 = strSplit1[i].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (strSplit2.Length > 1)
                {
                    alName.Add(strSplit2[0]);
                    alMarks.Add(strSplit2[1]);
                }
            }

string[] arrName = (string[])alName.ToArray(typeof(string)); string[] arrMarks = (string[])alMarks.ToArray(typeof(string));

If you need more help, please share sample/template file.

For converting ArrayList to Array, please use below lines of code

string[] arrName = (string[])alName.ToArray(typeof(string)); string[] arrMarks = (string[])alMarks.ToArray(typeof(string));

  • Related