streamreader newbie question

G

Guest

Hi,
I am trying to retrieve values from a text document in this format:

NewProd(0,0) = "xy004125"
NewProd(0,1) = "myTitle"

NewProd(1,0) = "xy004136"
NewProd(1,1) = "anotehrTitle"

NewProd(2,0) = "xy004115"
NewProd(2,1) = "yetAnotherTitle"

and so on...

Eventually i am going to try and pull out the product number and title and
put them in a xml file, but for now, i just want to retrieve the first group
of digits and put them in an array for further processing.

The problem is i can't get past the blank line. I keep getting the all the
zeros and then get a "Index was outside the bounds of the array" message.

Heres my code:

using (StreamReader sr = new StreamReader("test.inc"))
{
string sLine;
while ((sLine = sr.ReadLine()) != null)
{
if (sLine != null)
{
int firstParans = sLine.IndexOf('(');
firstParans = firstParans + 1;
char firstDig = sLine[firstParans];
Console.WriteLine(firstDig);
}
}
}

output is:

0
0

and then the error message mention above.

What am i missing?

Thanks
 
B

Barry Kelly

Ian M said:
I am trying to retrieve values from a text document in this format:

NewProd(0,0) = "xy004125"

I know this isn't of immediate help, but for this kind of problem it
would be very profitable to learn how regular expressions work
(System.Text.RegularExpressions). They can save you a lot of work by
using groups to extract just the data you want.

-- Barry
 
G

Guest

I got the answer. I just replaced

if (sLine != null)
{
int firstParans = sLine.IndexOf('(');
firstParans = firstParans + 1;
char firstDig = sLine[firstParans];
Console.WriteLine(firstDig);
}


with

int firstParans = sLine.IndexOf('(');
if (firstParans >=0)
{
firstParans += 1;
char firstDig = sLine[firstParans];
Console.WriteLine(firstDig);
}
 
G

Guest

Thanks Barry,
I actually played around with regular expressions and got some results
returned, but one of the programmers here encouraged me to use this method
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top