473,394 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. //using System.Text;
  4. using System.Collections.Generic;
  5.  
  6. namespace read
  7. {
  8.     class Program
  9.     {
  10.         public static void Read(/*string input*/)//comes later..
  11.         {
  12.             string input="BBC - Top Gear";//TESTCODE
  13.             List<string> lista = new List<string>();
  14.             string rad;
  15.             string temp1;
  16.             string temp2;
  17.             using (StreamReader latest = new StreamReader ("latest.txt"))
  18.             {
  19.  
  20.                 while ((rad = latest.ReadLine()) != null)
  21.                 {
  22.                     lista.Add(rad);
  23.                     Console.WriteLine(rad + "\nAntal tecken: " //TESTCODE
  24.                                       + rad.Length +"\n");       //TESTCODE
  25.  
  26.                     if (rad == input)
  27.                     {
  28.                         Console.WriteLine("rad == inkommande");//TESTCODE
  29.                         for (int i=9; i>=0; i--)
  30.                         {
  31.                             //string utgående = utgående.Add(rad);
  32.                             if (i==9)
  33.                             {
  34.                                 rad.Remove(9);
  35.                                 Console.WriteLine("Prints line(rad): ");
  36.                             }
  37.  
  38.                             Console.WriteLine("Moves position: ");
  39.                             for (int p=9; p>rad.Length; p--)
  40.                             {
  41.                                 rad.ToCharArray(temp1);
  42.                                 temp1=rad(p+1);
  43.                                 Console.WriteLine(rad);
  44.                             }
  45.                         }
  46.                     }
  47.  
  48.                     else
  49.                     {
  50.                         Console.WriteLine("rad != inkommande");//TESTCODE
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
Feb 10 '11 #1
1 1978
GaryTexmo
1,501 Expert 1GB
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!
Feb 10 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Michael Kirchner | last post by:
Hi everybody The output of my multiple dimension array is quite confusing. Im declaring an array, store some values in it and then I save the array in a session variable. On an other page I...
8
by: Magix | last post by:
Hi, I would like to use array of array to store different string, or if you have better idea to store different string in array index. How can I achieve that ? Any example will be greatly...
15
by: Yogi_Bear_79 | last post by:
Visual Studio .NET started complaing when the array was around 4000. I found that if I pasted the array in via notepad then opened Visual Studio it would work. Now my array is over 26,000 and...
1
by: Wild Wind | last post by:
Hello, I'm still trying to get to grips with managed C++. I want to raise an event RaiseEvent which has as one of its arguments a managed array. The event will be raised in a function Func...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
5
by: David | last post by:
Is there an effecient way to move an array entry up or down in the array? For example. Say you have an array of 5 entries. They are all strings by the way. myArray =...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
3
by: garyusenet | last post by:
Hello everyone I hope you are having a good evening. This evening I made a method that allows me to find the process id's of running processes of a given 'friendly name' - using...
3
by: JanHolland | last post by:
I tried: UPDATE `tablename` SET `fieldA` = `fieldB` WHERE fieldC = 'X'; in different combinations of fieldnames with/without _`_ I got: "Getroffen rijen: 0 (Query duurde 0.0011 sec)"....
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.