How to take content from file and put it into an array list?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Christian Sve
    New Member
    • Feb 2011
    • 1

    How to take content from file and put it into an array list?

    So I have got a problem, my program is not functioning as I want it to...

    I probably have a lot of errors in my code but what I want to do is that this code will take text from "latest.txt " (about five lines) and put it in a list. Later on, when this module is completed, my main program have a search function with a sort of "history of searches"(input ) and what I want is to make this module put the recent searched thing on index 0 of the list "rad" and increse the rest of the searches index with +1 and the last one that get index 10 will be removed.

    Please help me!

    Here's my code:
    Code:
    using System;
    using System.IO;
    //using System.Text;
    using System.Collections.Generic;
    
    namespace read
    {
    	class Program
    	{
    		public static void Read(/*string input*/)//comes later..
    		{
    			string input="BBC - Top Gear";//TESTCODE
    			List<string> lista = new List<string>();
    			string rad;
    			string temp1;
    			string temp2;
    			using (StreamReader latest = new StreamReader ("latest.txt"))
    			{
    				
    				while ((rad = latest.ReadLine()) != null)
    				{
    					lista.Add(rad);
    					Console.WriteLine(rad + "\nAntal tecken: " //TESTCODE
    					                  + rad.Length +"\n");	   //TESTCODE
    					
    					if (rad == input)
    					{
    						Console.WriteLine("rad == inkommande");//TESTCODE
    						for (int i=9; i>=0; i--)
    						{
    							//string utgående = utgående.Add(rad);
    							if (i==9)
    							{
    								rad.Remove(9);
    								Console.WriteLine("Prints line(rad): ");
    							}
    							
    							Console.WriteLine("Moves position: ");
    							for (int p=9; p>rad.Length; p--)
    							{
    								rad.ToCharArray(temp1);
    								temp1=rad(p+1);
    								Console.WriteLine(rad);
    							}
    						}
    					}
    					
    					else
    					{
    						Console.WriteLine("rad != inkommande");//TESTCODE
    					}
    				}
    			}
    		}
    	}
    }
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Reading the data out of the file is pretty straight forward, it looks like you've got that figured out and are able to get it into your string list. So the real question is how to get the rotating list of past searches, right?

    There's a few methods available to you on the List class that should help you out. Here's the MSDN documentation: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

    The methods you'll probably want to look at are Contains, Insert, Remove, and RemoveRange.

    Here's the general idea...

    You have your search string, s. If your list contains s, you can simply remove it. Then you can insert s at the beginning of the list. Once this has been done, check to see if the length of the list is greater than your maximum, at which point you can remove anything past that.

    If you're always updating your list you can simply just remove the item at index 10 (11th element), but when I did my test code I used RemoveRange just in case, for whatever reason, the list got an extra item inserted. That won't happen with a single insert, but your app sounds like it might get bigger so it's always good to prepare for if you might be inserting things to the list at other times.

    I hope that helps, please let me know if you have any questions!

    Comment

    Working...