Problem with streamreading into arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chiefy
    New Member
    • Nov 2007
    • 10

    Problem with streamreading into arrays

    Working in c#. This is my current code
    Code:
     string[] UID = new string[studentlength];
                    string[] RegNo = new string[studentlength];
                    int j = 0;
                    int k = 1;
    
                    for (int i = 0; i < words.Length; i=i+5)
                    {
                        UID[j++] = words[i];
                        RegNo[k++] = words[i];
    
                    }
    The variable "words" is an array that has been streamed from a file, the file is a series of lists. The two arrays named UID and RegNo are trying to take the different parts of the lists. UID works but RegNo causes an error because it overshoots the length of the array. Any ideas on how to fix this??
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I am a bit confused by what you are doing.
    UID and RegNo are getting the same value from words.
    As for why RegNo overshoots it's index while UID doesn't, it's because you start that index at 1 and not at 0.
    (Actually, using the ++ the way you do, UID starts at index 1 and RegNo starts at index 2, both should probably be starting at index 0)

    You are also ittertaing i by 5, yet your check condition does not take that into account.
    Try something like this:
    for (int i = 0; i < (words.Length-5); i=i+5)

    Comment

    • Chiefy
      New Member
      • Nov 2007
      • 10

      #3
      Originally posted by Plater
      I am a bit confused by what you are doing.
      UID and RegNo are getting the same value from words.
      As for why RegNo overshoots it's index while UID doesn't, it's because you start that index at 1 and not at 0.
      (Actually, using the ++ the way you do, UID starts at index 1 and RegNo starts at index 2, both should probably be starting at index 0)

      You are also ittertaing i by 5, yet your check condition does not take that into account.
      Try something like this:
      for (int i = 0; i < (words.Length-5); i=i+5)
      RegNo starts at 1 because it needs to hold the contents of the next list. For example
      UID RegNo
      123 2005
      124 2006
      125 2007
      Taking 5 off of the max for i will surely make it overshoot even more.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by Chiefy
        RegNo starts at 1 because it needs to hold the contents of the next list. For example
        UID RegNo
        123 2005
        124 2006
        125 2007
        Taking 5 off of the max for i will surely make it overshoot even more.
        That doesn't make any sense, I don't think you understand arrays correctly.

        I believe my original assumption was wrong, the index into words[] is not what is going out of bounds, I would say it is the indexes into UID and RegNo.


        Suppose studentlength=4 ;
        You now have UID[4] and RegNo[4].
        They each index at
        [0]
        [1]
        [2]
        [3]

        You start UID at [1], skipping [0]
        And you start RegNo at [2] skipping both [0] and [1].
        The indexes you skiped will not got populated with data.


        Set a break point before the loop and put the variables into your watch window and step through all the code watching the values change as you go.

        Comment

        Working...