Goody Goodmansen
Goody Goodmansen

Reputation: 13

How to read integers from a text file to array

So this is what I would like to do. I am kind of all over the place with this but I hope you can bear with me. This is a very new concept to me.

1) In my program I wish create an array of 50 integers to hold the data that comes from the file. My program must get the path to the user's Documents folder. 2) The name of the file will be "grades.txt". Code this file name right in your program. No user input is required to get the file name. 3) Create a StreamReader object, using this path. This will open the file. Write a loop that reads data from the file, until it discovers the end of the file. 4) As each integer value is read in, I display it, and store it in the array. 5) Using the concepts of partially filled arrays, write a method that takes the array as a parameter and calculates and returns the average value of the integers stored in the array Output the average.

So right now I am having a very hard time figuring out how to get the numbers saved in the grades.txt file, save them to an array, and display them. I try to split the integers and save them as that but it doesn't seem to work.

This is the code that I have so far:

class Program
{
    const int SIZE = 50;

    static void Main()
    {

        // This line of code gets the path to the My Documents Folder

        int zero = 0;
        int counter = 0;
        int n, m;
        StreamReader myFile;
        myFile = new StreamReader("C:/grades.txt");


        string inputNum = myFile.ReadLine();

        do
        {
            Console.Write("The test scores are listed as follows:");
            string[] splitNum = myFile.Split();
            n = int.Parse(splitNum[0]);
            {
                if (n != zero)
                {
                    Console.Write("{0}", n);                      
                    counter++;
                }
            }
        } while (counter < SIZE && inputNum != null);

        // now we can use the full path to get the document




        Console.ReadLine();
    }
}

This is the grades.Txt file:
88
90
78
65
50
83
75
23
60
94

Upvotes: 0

Views: 4307

Answers (2)

M.G.E
M.G.E

Reputation: 371

For reading the file you need something like this:

var scores = new List<int>();
        StreamReader reader = new StreamReader("C:/grades.txt");
        while (!reader.EndOfStream)
        {
            int score;
            if (int.TryParse(reader.ReadLine(), out score) && score != 0)
                scores.Add(score);
        }

and you can have count of scores with scores.Count property.

Upvotes: 1

Idle_Mind
Idle_Mind

Reputation: 39122

1) In my program I wish create an array of 50 integers to hold the data that comes from the file.

See Arrays Tutorial (C#).

2) My program must get the path to the user's Documents folder. The name of the file will be "grades.txt". Code this file name right in your program. No user input is required to get the file name.

Use these two:

Environment.GetFolderPath Method (Environment.SpecialFolder)

Path.Combine()

3) Create a StreamReader object, using this path. This will open the file. Write a loop that reads data from the file, until it discovers the end of the file.

See StreamReader.EndOfStream().

4) As each integer value is read in, I display it, and store it in the array.

If there is only one score per line, you don't need to do any Split() calls. Use your counter variable to know where in the Array to store the value.

5) Using the concepts of partially filled arrays, write a method that takes the array as a parameter and calculates and returns the average value of the integers stored in the array Output the average.

See Methods (C# Programming Guide).

You'd pass the Array and how many values are stored in it (the counter variable).

Upvotes: 0

Related Questions