Don't like ads? PRO users don't see any ads ;-)

CSV parser 2.0

By: bhalash on Apr 30th, 2012  |  syntax: C#  |  size: 4.91 KB  |  hits: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // CSV parser by Mark Grealish (s00123474@itsligo.ie).
  2.  
  3. using System;
  4. using System.IO;
  5. using System.Collections.Generic;
  6.  
  7. public class CsvReader
  8. {              
  9.         public static string path = "players.csv";
  10.  
  11.         static void Main()
  12.         {      
  13.                 int menuOpt = 42;      
  14.                
  15.                 do
  16.                 {
  17.                         Console.Clear();
  18.                         Console.WriteLine("1. View the contents of {0}.", path);
  19.                         Console.WriteLine("2. Search for a specific record in {0}.", path);
  20.                         Console.WriteLine();
  21.                         Console.WriteLine("4. Exit.");
  22.                         Console.WriteLine();
  23.                         menuOpt = IntParse("Selection: ");
  24.                        
  25.                         switch (menuOpt)
  26.                         {
  27.                                 case 1:
  28.                                         CsvOutput(path, -1);
  29.                                         break;
  30.                                 case 2:
  31.                                         CsvSearch(path);
  32.                                         break;
  33.                                 case 4:
  34.                                         break;
  35.                                 default:
  36.                                         break;
  37.                         }
  38.                 } while (menuOpt != 4 );
  39.         }
  40.  
  41.         // ++++LOAD CSV FILE++++
  42.         private static string[] CsvLoad(string path)
  43.         {      
  44.                 // Read the CSV file into a temp list, and then convert the list into a usable array.
  45.                 // The general philosophy here is to refresh the array whenever it is called for.
  46.                 List<string> temp       = new List<string>();
  47.                 string line             = "";
  48.  
  49.                 using (StreamReader csv = new StreamReader(path))
  50.                 {
  51.                         line = csv.ReadLine();
  52.  
  53.                         while (line != null)
  54.                         {
  55.                                 temp.Add(line);
  56.                                 line = csv.ReadLine();
  57.                         }
  58.                 }
  59.  
  60.                 string[] data = temp.ToArray();
  61.  
  62.                 return data;
  63.         }      
  64.  
  65.         // ++++VIEW CSV FILE+++
  66.         static void CsvOutput( string path, int rec )
  67.         {
  68.                 // Fill the array using the above method and output it per an input int:
  69.                 //              If -1, output the entire array.
  70.                 //              If >= 0, output that specific line.
  71.                 string[] csv            = CsvLoad(path);
  72.                 string[] row            = csv[0].Split(',');
  73.                 string[] header         = new string[4] {"Player #:", "Name:", "Score:", "Citation:"};
  74.                 string dash             = "---------------";
  75.                 string spacer           ="{0,-20}";
  76.  
  77.                 // Header.
  78.                 Console.Clear();
  79.                 for (int i = 0; i < header.Length; i++)
  80.                                 Console.Write(String.Format(spacer, header[i]));
  81.                 Console.WriteLine();
  82.                 for (int i = 0; i < header.Length; i++)
  83.                         Console.Write(String.Format(spacer, dash));
  84.  
  85.                 if (rec == -1)
  86.                 {
  87.                         // Output the entire array/CSV.
  88.                         for (int i = 0; i < csv.Length; i++)
  89.                         {
  90.                                 row     = csv[i].Split(',');
  91.                                 for (int j = 0; j < row.Length; j++)
  92.                                         Console.Write(String.Format(spacer, row[j]));
  93.                                         Console.Write(String.Format(spacer, Citation(row[2])));
  94.                         }
  95.                 }
  96.                 else if (rec >= 0)
  97.                 {
  98.                         // Output a single row.
  99.                         row     = csv[rec].Split(',');
  100.                         for (int i = 0; i < row.Length; i++)
  101.                                 Console.Write(String.Format(spacer, row[i]));
  102.                                 Console.Write(String.Format(spacer, Citation(row[2])));
  103.                 }
  104.                                                
  105.                 Back();
  106.         }
  107.        
  108.         // ++++SEARCH FOR A RECORD++++
  109.         static void CsvSearch(string path)
  110.         {
  111.                 string[] csv    = CsvLoad(path);               
  112.                 int n                   = 0;
  113.                 int s                   = 0;
  114.  
  115.                 Console.Clear();
  116.                 Console.Write( "Enter search string (case sensitive): " );
  117.                 string query = Convert.ToString(Console.ReadLine());
  118.  
  119.                 try
  120.                 {
  121.                         do
  122.                         {
  123.                                 s = csv[n].IndexOf(query);
  124.                         if ( s >= 0 )
  125.                                 break;
  126.                         n++;
  127.  
  128.                         } while ((n < csv.Length) );
  129.                 }
  130.                 catch (IndexOutOfRangeException)
  131.                 {
  132.                 }
  133.                                        
  134.                 if (s >= 0)
  135.                 {
  136.                         Console.Clear();
  137.                         Console.WriteLine("Record matched!");  
  138.                         CsvOutput( path, n );
  139.                 }
  140.                 else if (s < 0)
  141.                 {      
  142.                         Console.Clear();
  143.                         Console.WriteLine("String not matched, sorry. :(");
  144.                         Back();
  145.                 }
  146.         }
  147.  
  148.         // ++++CITATIONS++++
  149.         private static string Citation(string input)
  150.         {
  151.                 // Return a citation based on the player's score.
  152.                 string[] cit = new string [5] {
  153.                         "Sluggish Snail",
  154.                         "Ambling Armadillo",
  155.                         "Bobbing Bobcat",
  156.                         "Rocketing Rabbit",
  157.                         "Turbocharged Cheetah"
  158.                 };
  159.  
  160.                 int score = Convert.ToInt32(input);
  161.                 string citation = "";
  162.                                        
  163.                 if (score < 4000)
  164.                         citation = cit[0];
  165.                 if ((score >= 4000) && (score <= 5999))
  166.                         citation = cit[1];
  167.                 if ((score >= 6000) && (score <= 6999))
  168.                         citation = cit[2];
  169.                 if ((score >= 7000) && (score <= 9999))
  170.                         citation = cit[3];
  171.                 if ((score > 9999))
  172.                         citation = cit[4];
  173.                        
  174.                 return citation;
  175.         }
  176.  
  177.         // ++++GO BACK++++
  178.         static void Back()
  179.         {
  180.                 Console.WriteLine();   
  181.                 Console.Write("Press [RETURN] to return to the main menu.");
  182.                 Console.ReadLine();
  183.         }
  184.  
  185.         // ++++PARSE INT++++
  186.         private static int IntParse(string userPrompt)
  187.         {      
  188.                 bool evalInt = false;
  189.                 int parsedInt = 0;
  190.  
  191.                 do
  192.                 {
  193.                         Console.Write(userPrompt);
  194.                         evalInt = Int32.TryParse(Console.ReadLine(), out parsedInt);
  195.  
  196.                         if (!evalInt)
  197.                                 InputFail();
  198.  
  199.                 } while (!evalInt);
  200.  
  201.                 return parsedInt;
  202.         }
  203.  
  204.         // ++++PARSE DOUBLE++++
  205.         private static double DoubleParse(string userPrompt)
  206.         {      
  207.                 bool evalDouble = false;
  208.                 double parsedDouble = 0;
  209.  
  210.                 do
  211.                 {
  212.                         Console.Write( userPrompt );
  213.                         evalDouble = Double.TryParse(Console.ReadLine(), out parsedDouble);
  214.  
  215.                         if (!evalDouble)
  216.                                 InputFail();
  217.                                
  218.                 } while (!evalDouble);
  219.  
  220.                 return parsedDouble;
  221.         }
  222.        
  223.         // ++++ERROR++++       
  224.         static void InputFail()
  225.         {
  226.                 Console.WriteLine("Invalid input. Please enter a numerical value.");
  227.         }
  228. }