Facebook
From Josephine George Jebakumar, 9 Months ago, written in C#.
Embed
Download Paste or View Raw
Hits: 244
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Xml;
  12.  
  13. namespace Task_3
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.         /// <summary>
  22.         /// Opens File and executes code to show how many duplicates there are
  23.         /// </summary>
  24.         /// <param name="sender"></param>
  25.         /// <param name="e"></param>
  26.         private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
  27.         {
  28.             //Set up streamreader
  29.             StreamReader reader;
  30.             //Create filter for txt files
  31.             openFileDialog1.Filter = "Text Document|*.txt|All Files|*.*";
  32.             //List to store all names
  33.             List<string> namesList = new List <string> ();
  34.             //List to store duplicate names
  35.             List<string> duplicateCheckList = new List<string>();
  36.             //Try catch for input
  37.             try
  38.             {
  39.                 //If ok is pressed
  40.                 if(openFileDialog1.ShowDialog() == DialogResult.OK)
  41.                 {
  42.                     //Reads file
  43.                     reader = File.OpenText(openFileDialog1.FileName);
  44.                     //Try catch for input
  45.                     try
  46.                     {
  47.                         //Execute until the end of stream
  48.                         while (!reader.EndOfStream)
  49.                         {
  50.                             //Reads each line and stores into 2 lists and displays the name
  51.                             string line = reader.ReadLine();
  52.                             namesList.Add(line);
  53.                             duplicateCheckList.Add(line);
  54.                             listBoxAllNames.Items.Add(line);
  55.                         }
  56.                         //Set a counter for number of duplicates
  57.                         int counter = 0;
  58.                         //Goes through each name is original list
  59.                         foreach (string iNameList in namesList)
  60.                         {
  61.                             //Goes through each name in duplicate list
  62.                             foreach (string iDuplicateCheck in duplicateCheckList)
  63.                             {
  64.                                 //Checks if the original name list matches the duplicate list
  65.                                 if (iNameList == iDuplicateCheck)
  66.                                 {
  67.                                     //Counts the duplicate
  68.                                     counter++;
  69.                                 }
  70.                             }
  71.                             //Checks if a duplication has occured
  72.                             if (counter != 0)
  73.                             {
  74.                                 //Displays the dupliacte items with the number of duplicates
  75.                                 listBoxDuplicates.Items.Add(iNameList + ": " + counter);
  76.                             }
  77.                             //Removes all the duplicates from the duplicated list so it doesn't get show again
  78.                             duplicateCheckList.RemoveAll(x => x == iNameList);
  79.                             //Set counter to 0 for next
  80.                             counter = 0;
  81.                         }
  82.                     }
  83.                     catch (Exception ex)
  84.                     {
  85.                         //Displays error message
  86.                         MessageBox.Show(ex.Message);
  87.                         //Clears listboxes
  88.                         listBoxAllNames.Items.Clear();
  89.                         listBoxDuplicates.Items.Clear();
  90.                     }
  91.                 }
  92.             }
  93.             catch (Exception ex)
  94.             {
  95.                 //Displays error message
  96.                 MessageBox.Show(ex.Message
  97.                     );
  98.                 //Clears listboxes
  99.                 listBoxAllNames.Items.Clear ();
  100.                 listBoxDuplicates.Items.Clear();
  101.             }
  102.  
  103.         }
  104.     }
  105. }
  106.