Read a file to List<int>

1.2k views Asked by At

I am needing to create a list of numbers from an uploaded file, if the numbers have "-" representing a range, I need to split the numbers, making the first number a start, iterate through til it gets to the second number. I know I will be storing the numbers in a List, I'm just not sure on how to break out the numbers in the file with "-", Here is an example of the file:

099985-10043

102609-102886

102917-102921

106100-106101

110684-110685

114886-114887

117765-117766

120604-120605

121157-121158

121627-121911

122539

and here is where I am with the code:

 if(string.IsNullOrEmpty(fileName.Text)) return;
        _MissingInt = new List<int>();
        var lines = File.ReadAllLines(fileName.Text);
        foreach (string line in lines) {
            ...need help with logic...
        }

I would greatly appreciate any direction and help as my programming skills are pretty weak and I am learning...this is not homework

3

There are 3 answers

8
SarangK On BEST ANSWER

I didn't agree with Badiparmagi's answer as its adding string/character to int list, its not compilable code. Here giving you my tested attempt. I hope it may help you.

if (string.IsNullOrEmpty(fileName.Text)) return;
        var _MissingInt = new List<int>();
        var lines = File.ReadAllLines(fileName.Text);
        foreach (var line in lines)
        {
            if(string.IsNullOrEmpty(line)) 
                continue;
            if (line.Contains("-"))
            {
                var range = line.Split('-');
                int startNumber;
                int endNumber;
                if(int.TryParse(range[0], out startNumber) && int.TryParse(range[1]), out endNumber)
                   for (var i = startNumber; i < endNumber; i++)
                   {
                       _MissingInt.Add(i);
                   }
            }
            else
            {
                 int num;
                 if(int.TryParse(line, out num))
                    _MissingInt.Add(num);
            }
        }
0
Lajos Arpad On

I will assume that the file contains lines which can have two int values maximum, separated by -. Let's suppose we have a class like this:

class Interval {
    public int left;
    public int right;
    public bool hasRight;

    public Interval(int left, int right) {
        this.left = left;
        this.right = right;
        hasRight = true;
    }

    public Interval(int left) {
        this.left = left;
        hasRight = false;
    }
}

Now let's implement a parser method:

protected Interval parse(String line) {
    String[] parts = line.Split(new string[] {"-"});
    int left, right;
    if (!Int32.TryParse(parts[0], left)) {
        return null; //Invalid interval
    }
    return ((parts.length <= 1) || (!Int32.TryParse(parts[1], right))) ? (new Interval(left)) : (new Interval(left, right));
}

And another:

protected Interval[] aggregateParse(String[] lines) {
    Interval[] intervals = new Interval[lines.Length];
    for (int i = 0; i < lines.Length; i++) {
        intervals[i] = parse(lines[i]);
    }
    return intervals;
}

This could be used to generate intervals. If we need to get the integers between the edges of the interval and store them, then we can use a for cycle, starting from the left edge and ending at the right edge, filling an array of right - left - 1 size, which might be a member of interval. The problem is that an interval which is opened to the right will never end, so make sure you do this wisely.

0
sasha_gud On

Linq-style solution that supports all wrong input like strings and empty lines. For example:

1

sfd

2

5-10

11-fdfd

12 13

14

int x;

var res = lines
    // filter out empty lines
    .Where(line => !string.IsNullOrEmpty(line))
    // convert ranges to pairs
    .Select(line => line.Split('-'))
    // filter out not numbers
    .Where(line => line.All(number => int.TryParse(number, out x)))
    // convert all strings to int
    .Select(item => item.Select(int.Parse).ToList())
    .SelectMany(item =>
    {
        if (item.Count > 1)
        {
            return Enumerable.Range(item[0], item[1] - item[0] + 1);
        }

        return item;
    })
    .ToList();