streamreader (matching lines)
Sir/ma'am
I have a problem which is giving me fits.
I have a form that will return an arbitrary value of 1 to 1000 in textbox2. Once that value is returned, the user clicks a button to initiate a quarry of a file named test.txt(data below). The quarry will look for a line matching the value returned in textbox2. This program works like a charm if the value returned in textbox2 matches that of test.txt (e.g. if textbox2 equals 420 the following will result: textbox3.Text = rm125, textbox3.Text = rm131, and textbox3.Text =rm141)
However, if the value returned in textbox2 equals 421 then nothing will be returned in textbox 3 thru 5, obviously. I'm trying to determine a method for overcoming this obstacle. In theory, if 421 thru 424 were returned to textbox2, I would like it to return the same value as 420 (e.g. if textbox2 equals 421 thru 424 the following will result: textbox3.Text = rm125, extbox3.Text = rm131, and textbox3.Text =rm141). Is there a way to accomplish this task, without entering 1000 entries in test.txt?
I welcome any criticism or suggestions!
using (StreamReader readines = new StreamReader(@"C:\Program Files\test.txt"))
{
string line;
while ((line = readines.ReadLine()) != null)
{
Console.WriteLine(line); string[] lineStrings2 = line.Split(new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries); if (lineStrings2.Length > 1)
if (lineStrings2[0] == textbox2.Text)
{
textbox3.Text = lineStrings2[1];
textbox4.Text = lineStrings2[2];
textbox5.Text = lineStrings2[3];
// C:\Program Files\test.txt
;
[DATA]
;
16, rm12,rm13,rm14
21, rm11,rm133,rm141
415, rm124,rm130,rm140
420, rm125,rm131,rm141
425, rm126,rm132,rm142
theLizardPosted Dec 3, 2009, 6:50 PM
string a, b;
rs[] r = new rs[0];
int numberStart, numberEnd;
int index;
a = "6, rm12,rm13,rm14 21, rm11,rm133,rm141 415, rm124,rm130,rm140 420, rm125,rm131,rm141 425, rm126,rm132,rm142";
while(a.Length > 0)
{
Array.Resize(ref r, r.Length +1);
index = r.Length -1;
if(a.IndexOf(",") != -1)
b = a.Substring(0, a.IndexOf(","));
else
b = a;
if(b.IndexOf(" ") > 0) //there is a space in the sub string
{
r[index].a1 = b.Substring(0, b.IndexOf(" "));
if(r[index].a1.IndexOf("rm") != -1)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2).Trim();
r[index].b1 = b.Substring(b.IndexOf(" ")+1, b.Length - b.IndexOf(" ")-1);
r[index].myNumbers = new numb[0];
numberStart = Int16.Parse(r[index].a1);
numberEnd = Int16.Parse(r[index].b1);
int count = numberEnd - numberStart;
for(int j = 0; j < count+1; j++)
{
Array.Resize(ref r[index].myNumbers, r[index].myNumbers.Length+1);
r[index].myNumbers[j].number = numberStart + j;
}
}
else
{
r[index].a1 = b;
int jj = r[index].a1.IndexOf("rm");
if(r[index].a1.IndexOf("rm") != -1)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2).Trim();
}
if(a.IndexOf(",") != -1)
a = a.Remove(0, b.Length +1).Trim();
else
a = "";
b="";
}
theLizardPosted Dec 2, 2009, 10:44 PM
16, rm12,rm13,rm14 21, rm11,rm133,rm141 415, rm124,rm130,rm140 420, rm125,rm131,rm141 425, rm126,rm132,rm142
If the above is a single line in the text file then the best way to deal with this scenario is to read the whole line and use temp strings rather than split anything.
You could try the following code
//before constructor
struct rs
{
public string a1, b1;
}
//in button_click event or other
string a, b;
rs[] r = new rs[0]; //array of struct rs
int index; //index into array
//the text from your text file
a = "6, rm12,rm13,rm14 21, rm11,rm133,rm141 415, rm124,rm130,rm140 420, rm125,rm131,rm141 425, rm126,rm132,rm142";
//loop through string until lengthy of string is 0 (zero)
while(a.Length > 0)
{
Array.Resize(ref r, r.Length +1);
index = r.Length -1;
b = a.Substring(1, a.IndexOf(","));
if(b.IndexOf(" ") > 0) //there is a space in the sub string
{
r[index].a1 = b.Substring(1, b.IndexOf(" "));
if(r[index].a1.IndexOf("rm") > 0)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2);
r[index].b1 = b.Substring(b.IndexOf(" ")+1, b.Length - b.IndexOf(" "));
}
else
{
r[index].a1 = b;
if(r[index].a1.IndexOf("rm") > 0)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2);
}
a = a.Remove(1, b.Length);
b="";
}
Now you can compare the values of text box with the values stored in the array with
bool found = false;
for(int i = 0; i < r.Length; i++)
{
if(r[i].a1 == TextBox1.Text || r[i].b1.Text == TextBox2.Text)
found = true;
//other code you may want to use like
if(r[i].a1 == TextBox1.Text && r[i].b1.Text == TextBox2.Text)
found = true;
}
Now if you want to generate numbers between your sets like rm120 420 which is what I am now begining to understand what you are after you could do something like
//before constructor
struct numb
{
int number;
}
struct rs
{
public string a1, b1;
public numb[] myNumbers = new numb[0];
}
string a, b;
rs[] r = new rs[0];
int numberStart, numberEnd;
int index;
a = "6, rm12,rm13,rm14 21, rm11,rm133,rm141 415, rm124,rm130,rm140 420, rm125,rm131,rm141 425, rm126,rm132,rm142";
while(a.Length > 0)
{
Array.Resize(ref r, r.Length +1);
index = r.Length -1;
b = a.Substring(1, a.IndexOf(","));
if(b.IndexOf(" ") > 0) //there is a space in the sub string
{
r[index].a1 = b.Substring(1, b.IndexOf(" "));
if(r[index].a1.IndexOf("rm") > 0)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2);
r[index].b1 = b.Substring(b.IndexOf(" ")+1, b.Length - b.IndexOf(" "));
numberStart = Int16.Parse(r[index].a1);
numberEnd = Int16.Parse(r[index].b1);
int count = numberEnd - numberStart;
for(int j = 0; j < count; j++)
{
Array.Resize(ref r[index].myNumbers, r[index].myNumbers.Length+1);
r[index].myNumbers[j] = new int();
r[index].myNumbers[j] = numberStart + j;
}
}
else
{
r[index].a1 = b;
if(r[index].a1.IndexOf("rm") > 0)
r[index].a1 = r[index].a1.Remove(r[index].a1.IndexOf("rm"), 2);
}
a = a.Remove(1, b.Length);
b="";
}
I hope this makes sense to you...
Sam HobbsPosted Dec 2, 2009, 10:16 PM
First, note that you can use HTML in messages posted here, and you can use
at the end of lines to format code.
You need to use more than Split to process the data. Your data consists of two types of data; one type that is a single integer, such as 16, 21 and 415, and each one is followed by other data. You know that, but you must process the data so that the two types of data are processed differently. There are many ways to do that and I don't know the details of the fomat of the data. You might be able to use a Split that splits on spaces, then process that, and then use another split that splits on commas. Another possibility is regular expressions. It depends on the details of the data.