Foreach not looping through

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Paul73
    New Member
    • Sep 2007
    • 13

    Foreach not looping through

    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("i nput.in");
    StreamWriter sw3 = new StreamWriter("p roduction.out") ;
    StreamWriter sw4 = new StreamWriter("h old.out");
    string line;


    // READ SEARCH FILE INTO AN ARRAY

    StreamReader sr3 = new StreamReader("S earchFile.txt") ;

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

    }

    // LOOP THROUGH

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

    if (line.IndexOf(c ompare.ToString ()) == 0)
    {
    sw3.WriteLine(l ine);
    }
    else
    {
    sw4.WriteLine(l ine);
    }

    }
    }


    // 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.
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    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

    Comment

    • Paul73
      New Member
      • Sep 2007
      • 13

      #3
      Sorry. I've placed them inside the [CODE]

      Code:
      // 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();
      }
      
      }
      }

      Comment

      Working...