Jaqtaris
Jaqtaris

Reputation: 29

Text file to two string arrays in wpf using streamreader

I'm trying to read a text file to two string arrays. Array1 is to be all the odd lines, array2 all the even lines. I then add all the items of array1 to a combobox and when that is selected, or as it gets typed, outputs array2 to a textbox.

So far, I have tried a few methods from here, but the big issue seems to be creating the arrays. I tried to get help here before, but the answers didn't actually answer my question. They must be arrays, not lists (which I tried and worked well). I am really confused by this whole thing and my attempted code is now rubbish:

private void ReadFile(string filePath, string customerPhone, string customerName)
{
    string line = string.Empty;
    var fileSR = new StreamReader(filePath);
    bool number = true;

    while((line = fileSR.ReadLine()) != null)
    {
        if (number)
        {
            customerPhone(line);
            number = false;
        }
        else
        {
            customerName(line);
            number = true;
        }
   }

   fileSR.Close();
}

I'm losing confidence in this whole process, but I need to find a way to make it work, then I can learn why it does.

Upvotes: 0

Views: 453

Answers (3)

Jaqtaris
Jaqtaris

Reputation: 29

This worked! I have these class level strings:

string cFileName = "customer.txt";
string[] cName = new string[0];
string[] cPhone = new string[0];

And then this in the Window Loaded event, but could be used in it's own method:

private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
    //read file on start
    int counter = 0;
    string line;
    StreamReader custSR = new StreamReader(cFileName);
    line = custSR.ReadLine();

    while (custSR.Peek() != -1)
    {
        Array.Resize(ref cPhone, cPhone.Length + 1);
        cPhone[cPhone.Length - 1] = line;
        counter++;
        line = custSR.ReadLine();
        Array.Resize(ref cName, cName.Length + 1);
        cName[cName.Length - 1] = line;
        counter++;

        line = custSR.ReadLine();

        phoneComboBox.Items.Add(cPhone[cPhone.Length - 1]);
    }
    custSR.Close();

     //focus when program starts
     phoneComboBox.Focus();
}

Upvotes: 0

Hari Prasad
Hari Prasad

Reputation: 16966

You are almost there, just use the List<string>.

private void ReadFile(string filePath, string customerPhone, string customerName)
{
    string line = string.Empty;
    using (var fileSR = new StreamReader(filePath))
    {
        bool number = true;

        List<string> customerPhone = new List<string>();
        List<string> customerName = new List<string>();
        while((line = fileSR.ReadLine()) != null)
        {
            if (number)
            {
                customerPhone.Add(line);
                number = false;
            }
            else
            {
                customerName.Add(line);
                number = true;
            }
        }

        fileSR.Close();
    }
}

If you are interested only in Arrays, you could simply call customerName.ToArray() to convert it to an array.

Linq Solution

Alternatively you could use Linq and do this.

var bothArrays = File.ReadLines("filepath")        // Read All lines
    .Select((line,index) => new {line, index+1})   // index each line
    .GroupBy(x=> x/2)                              // Convert into two groups
    .SelectMany(x=> x.Select(s=>s.line).ToArray()) // Convert it to array
    .ToArray(); 

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186823

You should use collections to return data, say IList<String>:

private static void ReadFile(String filePath, 
  IList<String> oddLines, 
  IList<String> evenLines) {

  oddLines.Clear();
  evenLines.Clear();

  int index = 1; //TODO: start with 0 or with 1

  foreach (String line in File.ReadLines(filePath)) {
    if (index % 2 == 0)
      evenLines.Add(line);
    else
      oddLines.Add(line);

    index += 1; 
  }
}

using

  List<String> names = new List<String>();
  List<String> phones = new List<String>();

  ReadFile(@"C:\MyDate.txt", names, phones);

  // If you want array representation
  String[] myNames = names.ToArray();
  String[] myPhones = phones.ToArray();

  // Let's print out names
  Console.Write(String.Join(Envrironment.NewLine, names));

Please, notice, that using File.ReadLines usually more convenient than StreamReader which should be wrapped in using:

  // foreach (String line in File.ReadLines(filePath)) equals to
  using (var fileSR = new StreamReader(filePath)) {
    while ((line = fileSR.ReadLine()) != null) {
      ...
    }
  }

Upvotes: 0

Related Questions