CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Help with loop

  1. #1
    Join Date
    Dec 2011
    Posts
    14

    Help with loop

    Hi im trying to make a console application which reads 2 text files deletes the repeats words in the text file and writes it to one. Im trying to do 2 while loops inside each other the one reading a line at a time on the one file and the other reading all of the other file and seeing if the one is contained into the other.

    1. it will only loop through once
    2. when i change the if (strText2.Contains(strText1) == false) to if (strText2.Contains(strText1)) it writes the word that it containes so why wont it write the word that it doesnt contain??

    Code -

    Code:
     if (strMenu == "1")//Start conversion
                    {
                        Console.WriteLine("Please enter the file path of the 1st text file");
                        strFile1 = Console.ReadLine(); //set string to inputted file path
                        StreamReader sr = new StreamReader(strFile1); // new streamreader
                        Console.WriteLine("Please enter the file path for the second file");
                        strFile2 = Console.ReadLine();
                        StreamReader sr2 = new StreamReader(strFile2);
                        Console.WriteLine("Please enter the file path where you want the new text file to be saved");
                        strSaveFilePath = Console.ReadLine();
                        StreamWriter sw = new StreamWriter(strSaveFilePath);
                        sw.AutoFlush = true;
                        while (sr.ReadLine() != null)
                        {
                            strText1 = sr.ReadLine();
    
                            while (sr2.ReadLine() != null)
                            {
                                strText2 = sr2.ReadToEnd();
    
                                if (strText2.Contains(strText1) == false)
                                {
                                    Console.WriteLine(strText1);
                                    break;
                                }
                              
    
                            }
    
                        }

  2. #2
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    70

    Re: Help with loop

    It all depends on both list and how they are compared.

    For example
    (list1.Contains(wordFromList2)) == (list2.Contains(wordFromList1))
    but
    (!list1.Contains(wordFromList2)) != (!list2.Contains(wordFromList1))


    Here is a quick example illustrating what I mean.
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create 2 list
                string[] exampList1 = { "fruit", "friend", "dog", "cat", "people" };
                string[] exampList2 = { "fruit", "cat", "rat", "human", "pie"};
    
                Console.WriteLine("Example 1:");
                Console.WriteLine("Strings from list 1 found in list 2\n");
    
                // Loop goes through all strings found in list
                foreach (string s in exampList1)
                {
                    // Checks if string is found in the other list
                    if (exampList2.Contains(s))
                    {
                        // Writes out the string
                        Console.WriteLine(s);
                    }
                }
    
                Console.WriteLine("\n\nExample 2:");
                Console.WriteLine("Strings from list 1 not found in list 2\n");
    
                // Loop goes through all strings found in list
                foreach (string s in exampList1)
                {
                    // Checks if string is not found in other list
                    if (!exampList2.Contains(s))
                    {
                        // Writes out the string
                        Console.WriteLine(s);
                    }
                }
    
                Console.WriteLine("\n\nExample 3:");
                Console.WriteLine("Strings from list 2 found in list 1\n");
    
                // Loop goes through all strings found in list
                foreach (string s in exampList2)
                {
                    // Checks if string is not found in other list
                    if (exampList1.Contains(s))
                    {
                        // Writes out the string
                        Console.WriteLine(s);
                    }
                }
    
                Console.WriteLine("\n\nExample 4:");
                Console.WriteLine("Strings from list 2 not found in list 1\n");
    
                // Loop goes through all strings found in list
                foreach (string s in exampList2)
                {
                    // Checks if string is not found in other list
                    if (!exampList1.Contains(s))
                    {
                        // Writes out the string
                        Console.WriteLine(s);
                    }
                }
    
                // Wait for user input before closing application
                Console.Write("\n\nPress any key to continue...");
                Console.ReadKey();
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured