0

I have the following code below that reads in text file and searches it for integers. I am using 'int.TryParse' to do this but it is not storing the integer values in the lists after it has run, just wondering if you could tell me what is wrong with this code. Thanks.

 namespace AccessErrorFile
  {
    class Program
    {
        static void Main(string[] args)
        {
            List<int> plans = new List<int>();
            List<int> events = new List<int>();

            using (var reader = new StreamReader(@"D:\Temp\AccessEmail.txt"))

                try
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        //split the line
                        string[] parts = line.Split(new[] { "Event" },      StringSplitOptions.None);

                        //get valid integers
                        plans.Add(GetInt(parts[0].Split(' ', '\'')));
                        events.Add(GetInt(parts[1].Split(' ', '\'')));                    
                   }
                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("Error" + ex.Message);
                }

               //print the elements in the lists
               foreach (int x in plans)
               {
                   Console.WriteLine(x);
               }

               foreach (int y in events)
               {
                   Console.WriteLine(y);
               }

               //print the number of elements in the lists
               Console.WriteLine(plans.Count);
               Console.WriteLine(events.Count);
               Console.ReadLine();
        }    
        public static int GetInt(string[] a)
        {
            int i = 0;
            foreach (string s in a)
            int.TryParse(s, out i);    
            return i;
        }         
    }
}
3
  • 3
    Did you use your debugger to inspect what kind of values you're attempting to parse? How can we possibly tell you why TryParse is failing if you don't even tell us what it's attempting to parse? Commented Nov 6, 2014 at 17:50
  • use the Debugger. it will help you see what's happening Commented Nov 6, 2014 at 17:52
  • Hi tnw, I am parsing lines like this in the text file: "Previous errors were for Plan id '1111111' Event id '33234042'" Commented Nov 6, 2014 at 18:05

1 Answer 1

3

your problem is

  public static int GetInt(string[] a)
    {
        int i = 0;
        foreach (string s in a) //HERE
            int.TryParse(s, out i); //AND HERE 

        return i;
    }   

you are parsing in the loop, so the return i statement returns basically the last s of iteration, which, most probably is not a number, so i=0.

If you want to add all numbers from the string to array of ints, you can do something like:

IEnumerable<int> GetNumbersFromList(string[] s) {

    foreach(var str in s) {
        int val;        
        if(int.TryParse(str, out val)) 
            yield return val;
    }
}

and after

  plans.AddRange(GetNumbersFromList(parts));

Just a basic idea, naturally, you have to fit it to your needs.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Tigran, what would be the solution for this, it needs to loop like this I think as the line is in this format: "Previous errors were for Plan id '1111111' Event id '33234042'"
@John: you create an array of string and feed it into the this function. it roll overs the array of string during iteration and generates ints available inside an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.