473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Foreach not looping through

13
First off...thank you all for this forum. I've lurked for quite a while and you've all been so helpful. It's a life saver for someone like me.

Now here's my problem. What I'm trying to do is search each record in a file for an id number. When it finds the number it saves it to production.out. When it doesn't find it it saves it to hold.out.

The id numbers are stored in a separate text file so anyone can edit it.

So I'm reading it into an arraylist then using foreach to iterate through. The problem is it only reads the first number and ignores the others. Does anyone see why?


// CREATE OUTPUT FILES

StreamReader sr2 = new StreamReader("input.in");
StreamWriter sw3 = new StreamWriter("production.out");
StreamWriter sw4 = new StreamWriter("hold.out");
string line;


// READ SEARCH FILE INTO AN ARRAY

StreamReader sr3 = new StreamReader("SearchFile.txt");

ArrayList idNumber = new ArrayList();
string lineval2 = "";
while ((lineval2 = sr3.ReadLine()) != null)
{
idNumber.Add(lineval2);

}

// LOOP THROUGH

foreach ( string compare in idNumber)
{
while ((line = sr2.ReadLine()) != null)
{

if (line.IndexOf(compare.ToString()) == 0)
{
sw3.WriteLine(line);
}
else
{
sw4.WriteLine(line);
}

}
}


// CLEAN UP

sw3.Flush();
sw3.Close();
sr2.Close();
sw4.Flush();
sw4.Close();
}

}
}


The results of the code above would be something like this. Say the external says this:

id1
id2
id3

It will save all the records that contain id1 into the production.out file and will save everything else into hold.out instead of saving them all to production.out.
Sep 14 '07 #1
2 1231
Shashi Sadasivan
1,435 Expert 1GB
Hi,
Please try placing your code within CODE tags.

I see that you have a foreach loop outside the while loop of reading sr2.
Hope you know that when the foreach goes to the next string, sr2 will not be rewound. which means that only the first string is being compared with all the entries in sr2.

Cheers
--
PS: You might consider putting the foreach loop within the while loop
Sep 15 '07 #2
Paul73
13
Sorry. I've placed them inside the [code]

Expand|Select|Wrap|Line Numbers
  1. // CREATE OUTPUT FILES
  2.  
  3. StreamReader sr2 = new StreamReader("input.in");
  4. StreamWriter sw3 = new StreamWriter("production.out");
  5. StreamWriter sw4 = new StreamWriter("hold.out");
  6. string line;
  7.  
  8.  
  9. // READ SEARCH FILE INTO AN ARRAY
  10.  
  11. StreamReader sr3 = new StreamReader("SearchFile.txt");
  12.  
  13. ArrayList idNumber = new ArrayList();
  14. string lineval2 = "";
  15. while ((lineval2 = sr3.ReadLine()) != null)
  16. {
  17. idNumber.Add(lineval2);
  18.  
  19. }
  20.  
  21. // LOOP THROUGH
  22.  
  23. foreach ( string compare in idNumber)
  24. {
  25. while ((line = sr2.ReadLine()) != null)
  26. {
  27.  
  28. if (line.IndexOf(compare.ToString()) == 0)
  29. {
  30. sw3.WriteLine(line);
  31. }
  32. else
  33. {
  34. sw4.WriteLine(line);
  35. }
  36.  
  37. }
  38. }
  39.  
  40.  
  41. // CLEAN UP
  42.  
  43. sw3.Flush();
  44. sw3.Close();
  45. sr2.Close();
  46. sw4.Flush();
  47. sw4.Close();
  48. }
  49.  
  50. }
  51. }
Sep 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Cappy | last post by:
I am writing an XML menu structure. I have the following XML file <MenuItems> <Item> <Name>Homepa5ge</Name> <URL>/index.aspx</URL> <Alt>Return to homepage</Alt>...
2
by: Claire Reed | last post by:
Dear All, I am repeatedly encountering a problem whilst looping through XML Nodes and I am unsure as to what is going on and how I can get around it. I load the following XML document into an...
1
by: Diva | last post by:
Hi, I have a data grid in my application. It has 20 rows and I have set the page size as 5. I have a Submit button on my form and when I click on Submit, I need to loop through the rows in the...
4
by: Sjoerd | last post by:
Summary: Use foreach(..) instead of while(list(..)=each(..)). --==-- Foreach is a language construct, meant for looping through arrays. There are two syntaxes; the second is a minor but useful...
2
by: randy1200 | last post by:
My hope is that somebody has a thought on this, or can point to toward an article that's useful. I have the following: DataSet ds = new DataSet(); ds.ReadXml("MyData.xml"); Each MainTable...
2
by: Bart Van Hemelen | last post by:
The situation: I have a CheckBoxList cblTest, the items are disabled in cblTest_DataBound in a foreach (ListItem oItem in cblTest.Items) loop. I provide a link that calls a client-side JavaScript...
7
by: Mick Walker | last post by:
I have a collection of objects populated from a SQL Server (sorted descending). Each of these object has a date field. I need a way to loop through these object starting from the newest entry,...
0
by: jags_32 | last post by:
We have a pretty simple data flow that fetches data from our Source ERP system and dumps it into a SQL Server table. This functionality works, what we are trying to do now is to extend this...
3
by: mrsouthg | last post by:
Hi, I am fairly new to c# .net language and have been using a set of code I was given to learn from. I have found a suitable part which I would like to change, I am however finding it a big hurdle...
24
by: PrezWeezy | last post by:
I am attempting to create a loop to read through each XML attribute in a node and put it into a datatable. I have created my datatable with columns that are the same name as the attributes in my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.