reading data from txt file

hi there i have txt file that i reading from him

the txt file is like this :

150         -6
140        -10
150-5     -15

the first number is the product key and the -6 is the quantity that sold in day.
i need to poll out the proKey to a string variable and the quantity to a int variable
then i need to send them to a func

i tryd to do it like that :

      string sLine="";
                  ArrayList arrText = new ArrayList();
                  char delimiter = ' ';
            while (sLine != null)
                  {
                        sLine = objReader.ReadLine();
                        if (sLine != null)

                              arrText.Add(sLine);
                        
                  }
                  objReader.Close();

                  foreach (string sOutput in arrText)
                        MessageBox.Show(sOutput);
but i am geeting the ProKey and the quntaty in the same line i need to split them into a 2 varible...

thanks ...
Tech_MenAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bruintjeCommented:
Hello Tech_Men,

those spaces you are looking for are probably tabs you could try it with

char delimiter = (char)Keys.Enter;

as the delimiter, and i miss the split on the delimiter too in this code snippet you posted but having the tab will at least correct the empty space i think

hope this helps a bit
bruintje
Tech_MenAuthor Commented:
how can i use the delimter and where in the code ?
bruintjeCommented:
sorry, thought you already had more code

        private void button1_Click(object sender, EventArgs e)
        {

            StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
            string str1;
            object[] str2;
            object[] str3;
            str1 = objReader.ReadToEnd();
            objReader.Close();
            str2 = str1.Split('\n');
            foreach (object v in str2)
            {
                str3 = v.ToString().Split('\t');
                foreach (object v2 in str3)
                {
                    MessageBox.Show(v2.ToString());
                }
            }
        }
 
this will first split the file on linefeed '/n'
then each line on a tab character '/t'
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Fernando SotoRetiredCommented:
Hi Tech_Men;

Here is a solution using Regular Expression.

Imports System.IO
Imports System.Text.RegularExpressions

        Dim sr As New StreamReader("C:\Temp\ParseNums.txt")
        Dim input As String = sr.ReadToEnd()
        sr.Close()
        Dim re As New Regex("(?<ProdKey>[\d-]+)\s+(?<Quant>-\d+)", _
            RegexOptions.Compiled)
        Dim mc As MatchCollection

        mc = re.Matches(input)
        For Each m As Match In mc
            ' Get the information from the Regex object
            Dim ProdKey As String = m.Groups("ProdKey").Value
            Dim quantitySold As Integer = CInt(m.Groups("Quant").Value)

            MessageBox.Show("Product Key = " & ProdKey & " Quantity Sold = " & _
                quantitySold.ToString())
        Next


Let me know if you have any questions.

Fernando
bruintjeCommented:
here is my latest on this one, ligths are going out :)

this will add all split lines as array to the arraylist and to see if that works i added a new string array at the end with 2 messageboxes

        private void button1_Click(object sender, EventArgs e)
        {

            StreamReader objReader = new StreamReader("c:\\" + @"\testFile.txt");
            string str1;
            object[] str2;
            string[] str3;
            int i=0;
            ArrayList arrText = new ArrayList();
            str1 = objReader.ReadToEnd();
            objReader.Close();
            str2 = str1.Split('\n');
            foreach (object v in str2)
            {
                str3 = v.ToString().Split('\t');
                arrText.Add(str3);
                string[] mystringarray = (string[])arrText[0];
                MessageBox.Show(mystringarray[0]);
                MessageBox.Show(mystringarray[1]);                
            }
        }
    }
Fernando SotoRetiredCommented:
Sorry Tech_Men posted last solution in the wrong language. Here it is in C#.

using System.IO;
using System.Text.RegularExpressions;

      StreamReader sr =new StreamReader(@"C:\Temp\ParseNums.txt");
      string input = sr.ReadToEnd();
      sr.Close();
      Regex re = new Regex(@"(?<ProdKey>[\d-]+)\s+(?<Quant>-\d+)",
            RegexOptions.Compiled);
      MatchCollection mc;

      mc = re.Matches(input);
      foreach( Match m in mc )
      {
            // Get the information from the Regex object
            string ProdKey = m.Groups["ProdKey"].Value;
            int QuantitySold = Convert.ToInt32(m.Groups["Quant"].Value);

            MessageBox.Show("Product Key = " + ProdKey + " Quantity Sold = " +
                  QuantitySold.ToString());
      }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tech_MenAuthor Commented:
thanks !!!
Fernando SotoRetiredCommented:
Glad I was able to help. :=)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.