Home > Software engineering >  List.Remove All not removing the negative numbers from textfile
List.Remove All not removing the negative numbers from textfile

Time:02-26

Here's what I want this code to do.

  1. Read the textfile random.txt into a List
  2. For each line of the textfile read into the list I want to determine if it is positive or negative using .RemoveAll with a lambda expression.
  3. RemoveAll should remove each line containing a negative number
  4. I want to display the changed list into a listbox to display.

I cannot use Linq, and I must use the ListMethod RemoveAll.

'''''

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.IO;

 namespace meade_13_1
 {
  public partial class Form1 : Form
 {

    public Form1()
    {
        InitializeComponent();

    }
    private void btnFindAll_Click(object sender, EventArgs e)
    {
       
    }

    private void btnRemoveNeg_Click(object sender, EventArgs e)
    {
        List<int> list = new List<int>();
        using (StreamReader reader = new StreamReader("random.txt"))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                list.Add(Int32.Parse(line));
                
                
            }
        }
        list.RemoveAll(x => x > 0);
        listBox1.Items.Add(list);

         }
     }
 }

'''''

CodePudding user response:

Instead of working with reader, list directly, I suggest quering with a help of Linq:

using System.Linq;

...

private void btnFindAll_Click(object sender, EventArgs e) {
  var items = File
    .ReadLines("random.txt")
    .Where(line => !string.IsNullOrWhiteSpace(line))
    .Select(line => double.Parse(line)) //TODO: int.Parse if item is int
    .Where(item => item > 0); 

  foreach (var line in items) 
    listBox1.Items.Add(line);        
}

Here I've added .Where(line => !string.IsNullOrWhiteSpace(line)) to be on the safe side of the rode - I ignore possible empty lines

CodePudding user response:

It doesn't remove negative numbers simply because the lamda expression you pass to RemoveAll is used to filter positive numbers. For negatives it should be x => x < 0.

So if you need the version without Linq, I can propose two options:

  1. Without list at all (just add to the listbox only positive numbers)
using (StreamReader reader = new StreamReader("random.txt"))
{
   string line;
   while ((line = reader.ReadLine()) != null)
   {
    int number = int.Parse(line);

    if (int.TryParse(line, out int number) && number >= 0)
       listBox1.Items.Add(number);
   }
}
  1. With list and RemoveAll
var numbers = new List<int>();
using (StreamReader reader = new StreamReader("random.txt"))
{
   string line;
   while ((line = reader.ReadLine()) != null)
   {
     if (int.TryParse(line, out int number)
       numbers.Add(number);
   }
   numbers.RemoveAll(n => n < 0);
   foreach (int num in numbers)
     listBox1.Items.Add(num);
}

And by the way for this kind of task the File.ReadLines is more natural (because even the method name says that this does exactly what we need):

foreach (string line in File.ReadLines("random.txt")
{
  if (int.TryParse(line, out int num) && num > 0)
     listBox1.Items.Add(num);
}
  • Related