-1

I'm taking data from multiple text files using streamReader and putting it together into one text file using StreamWriter in c#, visual studio. Each text file has multiple lines, each line contains data entered by a user, so the number of lines changes over time. The streamwriter is correctly taking data from the streamreader and putting it into the text file, but only for one line, and then it stops working. Why is this?

It also calculates another variable but that seems to be unrelated, I'll include it anyway though just in case.

My Code:

 using (StreamWriter sw = new StreamWriter("groups.txt"))
            {
                for (int l = 0; l < pupilDetailsID.Count; l++)
                { 
                    for (int i = 0; i < skiTimeID.Count - 1; i++)
                    {
                        for (int j = 0; j < quizID.Count; j++)
                        {

                            if (skiTimeID[i] == quizID[j] && quizID[j] == pupilDetailsID[l])
                            {
                                string fullPupilRecord = pupilDetailsID[l] + "~" + pupilDetailsFirstName[l] + "~" + pupilDetailsSurname[l] + "~" + pupilDetailsClass[l]
                                + "~" + pupilDetailsNumber[l] + "~" + skiTime1[i] + "~" + skiTime2[i] + "~" + skiTime3[i] + "~" + skiTime4[i] + "~" + skiTime5[i] + "~" + skiTime[i] + "~"
                                + quizScore[j] + "~" + quizPercentage[j];


                                int quizScoreNumber = Convert.ToInt32(quizScore[j]);
                                decimal skiTimeDecimal = Convert.ToDecimal(skiTime[i]);

                                if (quizScoreNumber >= 7 && skiTimeDecimal <= 40)
                                {
                                    groupLevel = "Advanced";
                                }

                                else if (quizScoreNumber >= 9 && skiTimeDecimal <= 50)
                                {
                                    groupLevel = "Advanced";
                                }

                                else if (quizScoreNumber >= 5 && skiTimeDecimal <= 30)
                                {
                                    groupLevel = "Advanced";
                                }

                                else if (quizScoreNumber >= 5 && skiTimeDecimal <= 50)
                                {
                                    groupLevel = "Intermediate";
                                }

                                else if (quizScoreNumber >= 7 && skiTimeDecimal <= 60)
                                {
                                    groupLevel = "Intermediate";
              
                                }

                                else if (quizScoreNumber >= 4 && skiTimeDecimal <= 40)
                                {
                                    groupLevel = "Intermediate";
                                }


                                else
                                {
                                    groupLevel = "Beginner";
                                }

                                fullPupilRecord = fullPupilRecord + "~" + groupLevel;
                                sw.WriteLine(fullPupilRecord);
                                fullPupilRecord = "";
                            }
                        }
                    }   
                }
                sw.Close();
            }

I don't really understand my own code, or c# in general (don't ask why I put myself in a position where i had to code, i know, i made a dumb decision but its too late now) so it would be appreciated if the answer was something i can copy and paste into my code to fix it, if possible

  • welcome to coding - dont give up ;-). Just guessing here, but your code only calls WriteLine when if (skiTimeID[i] == quizID[j] && quizID[j] == pupilDetailsID[l]) , so maybe this statement is only true once in your data – Rick Hodder Apr 06 '22 at 19:52
  • BTW, you dont need to call sw.Close() - your using statement will automatically close and dispose of the StreamWriter – Rick Hodder Apr 06 '22 at 19:53
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/11838196) – srk Apr 06 '22 at 20:31
  • I figured out the problem, still don't know how to fix it. It's reading every line except the last, I just didn't think to test with more than 2 lines for some reason. How to stop it from excluding the last line. Also I don't think a debugger will help because I think it's doing the wrong thing sucessfully – ShadySpiritomb Apr 06 '22 at 22:04
  • Why do you have a `- 1` here? `for (int i = 0; i < skiTimeID.Count - 1; i++)`. That seems suspect to me since normally you'd just use `i < skiTimeID.Count`. if you're starting at zero. The `- 1` part would make it skip the last element... – Idle_Mind Apr 07 '22 at 02:02

1 Answers1

0

So we through discussion, the solution is:

If you want to not exclude the last row, you should modify your judgment condition.

Modify for (int i = 0; i < skiTimeID.Count - 1; i++) to for (int i = 0; i < skiTimeID.Count; i++).

Jiale Xue - MSFT
  • 3,560
  • 1
  • 6
  • 21