CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2011
    Posts
    9

    [RESOLVED] Delimiter while loop error!

    Ive been attempting to complete a project which is due in a number of hours (yeah im an idiot leaving it this late) where I have to take information on properties.

    Ive been trying to make a graph on how many times each county is entered. The information is stored on a text file with "," delimiters. Every 8th value should be the name of a county. The code ive been using is stated below:

    using (StreamReader sr = new StreamReader("base.txt"))
    {
    while ((line = sr.ReadLine()) != null)
    {
    string[] delimiters = new string[] { "," };
    while(delimiters[i] != null)
    {
    if(delimiters[i] == "Antrim")
    {
    antrimPoints++;
    }
    else if(delimiters[i] == "Armagh")
    {
    armaghPoints++;
    }

    else if(delimiters[i] == "Down")
    {
    downPoints++;
    }

    else if(delimiters[i] == "Fermanagh")
    {
    fermanaghPoints++;
    }

    else if(delimiters[i] == "LDerry")
    {
    lderryPoints++;
    }

    else if(delimiters[i] == "Tyrone")
    {
    tyronePoints++;
    }

    else
    {
    lostPoints++;
    }

    i = i + 8;


    }

    Console.WriteLine(line);
    }
    }

    The error message I am getting it:
    "IndexOutOfRangeException was unhandled." And the part highlighted is: while(delimiters[i] != null)

    Is there something wrong with this statement? Im really struggling to get this form working. Gah, im such a programming newb.

  2. #2
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Delimiter while loop error!

    You create
    string[] delimiters = new string[] { "," };

    This creates an array of only one item i.e. ","
    so when you access delimiters[i] and i > 0 you will get an error.

    Have a look at this to see how to create arrays:
    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx

  3. #3
    Join Date
    Apr 2011
    Posts
    9

    Re: Delimiter while loop error!

    Man thats a clutzy mistake. For some reason I thought that piece of code would do it all for me. Thanks for the advise.

    Im trying to read the text file into an array automatically. Is there any easy way to do that? I don't see any information on that in your links.

    And on a side note. Im using Visual Studio 2008. Is there any way to remove the "x" button from the top right of my forms? Id like the users to have to use an in-form button to exit the application.

  4. #4
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Post Re: Delimiter while loop error!

    For the X button change the ControlBox property on the form to False. Can you post an example of the file you're trying to read in and I'll take a look at converting it into an array for you.

  5. #5
    Join Date
    Apr 2011
    Posts
    9

    Re: Delimiter while loop error!

    So far here's what contained in the text file:

    236454,Antrim,Belfast,Semi-Detached,3,30000,John,Cheap
    385960,Tyrone,Eniskillin,Terraced,4,45000,Eddie,Small

    However more lines will be added, each time someone adds a new property.

  6. #6
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Delimiter while loop error!

    Ok. Try this out

    Code:
    //Indicate what position in each line the county appears in
                const int COUNTY_INDEX = 1;
    
                //Prepare our delimiter
                char[] delimiter = new char[] { ',' };
    
                //Prepare our dictionary for storing a count for each county
                Dictionary<string, int> countyCount = new Dictionary<string, int>();
    
                using (StreamReader sr = new StreamReader("base.txt"))
                {
                    do
                    {
                        //Extract the next line from the file
                        //and pull out the county
                        string line = sr.ReadLine();
                        string[] parts = line.Split(delimiter);
                        string county = parts[COUNTY_INDEX];
    
                        //Make a note of the count for this county
                        if (countyCount.ContainsKey(county))
                        {
                            countyCount[county]++;
                        }
                        else
                        {
                            countyCount.Add(county, 1);
                        }
    
                    } while (!sr.EndOfStream);
                }
    
                foreach (KeyValuePair<string, int> county in countyCount)
                {
                    string name = county.Key;
                    int number = county.Value;
    
                    //***** Now do what you like with name and number
                }

  7. #7
    Join Date
    Apr 2011
    Posts
    9

    Re: Delimiter while loop error!

    Ive entered the code and my form no longer errors. Im not sure how exactly to access each counties count however. Sorry for sounding a bit silly.

    If say twelve antrim properties had been entered, would "antrim.Value" display the number 12? Once I have access to the number I can definitely make the bar charts easily enough.

    Thanks for all the help so far!

  8. #8
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Delimiter while loop error!

    All the data you need is in the countyCount dictionary.

    Put a break point in the final foreach loop I wrote and you'll see that the name variable will step through the names our your counties and the number will be the count for that county.

    If you want to access Antrim directly then use
    countyCount["Antrim"]
    This will return an int with the count.

    I wouldn't recommend going down that route of hard coding 'Antrim' in your code because your code won't be very flexible. Better off looping through the dictionary and building your graph from that.

  9. #9
    Join Date
    Apr 2011
    Posts
    9

    Re: Delimiter while loop error!

    Thanks man. That works perfectly. You've literally saved me from failing. Its works and the graph works.

  10. #10
    Join Date
    Mar 2011
    Location
    London
    Posts
    54

    Re: Delimiter while loop error!

    Excellent. That's good news. Please mark this thread as 'Resolved'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured