Zoro
Zoro

Reputation: 719

Index out of Range with my values

I am attempting to print off the times 'NONE appears, as I have all my other information printing off accurate. I believe it may be something I am doing with my values but every time i fix something and get my code to compile, I keep getting zero's as my answer. Why is this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication33
{
class Program
{
    static void Main(string[] args)
    {
        FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
        StreamReader inFile = new StreamReader(fStream);
        string inValue;
        string[] values;
        double GPA;
        double total = 0;
        double counter = 0;
        double count = 0;
        double counti = 0;
        List<string> Anderson = new List<string>(); //Anderson
        List<string> gpa = new List<string>(); //GPA
        List<string> noemail = new List<string>(); // email

        while (!inFile.EndOfStream)
        {
            inValue = inFile.ReadLine();
            values = inValue.Split(" ".ToCharArray());
            if (inValue.StartsWith("(LIST (LIST "))
            {
                values = inValue.Split(" ".ToCharArray());
                GPA = double.Parse(values[8]);
                total = total + GPA;
                counter++;
            }
            if (values[2] == "'Anderson")
            {
                Anderson.Add(values[2]);
                count++;
            }
            if (values[2] == " ")
            {
                counter++;
            }
            if (values[6] == "'NONE")
            {
                noemail.Add(values[6]);
                counti++;
            }

        }
        double average = (double)total / (double)counter;
        Console.WriteLine("The average gpa is..." + average.ToString());
        Console.WriteLine("Total last names with Anderson is..." + count.ToString());
        Console.WriteLine("Total number of students is..." + counter.ToString());
        Console.WriteLine("Total is..." + counti.ToString());
    }
}
}

Upvotes: 0

Views: 214

Answers (3)

Jun Wei Lee
Jun Wei Lee

Reputation: 1022

You are off by one when trying to figure out whether an email is present or not

FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;
double GPA;
double total = 0;
double counter = 0;
double count = 0;
double counti = 0;
double countNoTelephone = 0;
List<string> Anderson = new List<string>(); //Anderson
List<string> gpa = new List<string>(); //GPA
List<string> noemail = new List<string>(); // email

while (!inFile.EndOfStream)
{
    inValue = inFile.ReadLine();

    if (inValue.StartsWith("(LIST (LIST "))
    {
        values = inValue.Split(" ".ToCharArray());
        GPA = double.Parse(values[8]);
        total = total + GPA;
        counter++;

        if (values[2] == "'Anderson")
        {
            Anderson.Add(values[2]);
            count++;
        }
        if (values[2] == " ")
        {
            counter++;
        }
        if (values[7] == "'NONE") // you got this off by one
        {
            noemail.Add(values[6]);
            counti++;
        }

        if (values[6] == "'NONE") // you are not counting this
        {
            countNoTelephone++;
        }
    }
}
double average = total / counter;
Console.WriteLine("The average gpa is..." + average);
Console.WriteLine("Total last names with Anderson is..." + count);
Console.WriteLine("Total number of students is..." + counter);
Console.WriteLine("Total with no emails is..." + counti);
Console.WriteLine("Total with no telephone is..." + countNoTelephone);
Console.WriteLine("Total with no telephone or emails are..." + (countNoTelephone + counti)); // add both no telephone and no emails together
Console.ReadKey();

A better way to do this is to serialize your text file into .NET objects, and then perform calculations on them. Have a look at this

public class Student
{
    public string LastName { get; set; }
    public string FirstName { get; set; }
    public string Code { get; set; } // not sure what the J M T K code is
    public string Telephone { get; set; }
    public string Email { get; set; }
    public double Gpa { get; set; }
}

private static void Main(string[] args)
{
FileStream fStream = new FileStream("Students.txt", FileMode.Open, FileAccess.Read);
StreamReader inFile = new StreamReader(fStream);
string inValue;
string[] values;

List<Student> students = new List<Student>();

while (!inFile.EndOfStream)
{
    inValue = inFile.ReadLine();

    if (inValue.StartsWith("(LIST (LIST "))
    {
        values = inValue.Split(" ".ToCharArray());

        Student student = new Student();

        student.LastName = values[2];
        student.FirstName = values[3];
        student.Code = values[4];
        student.Telephone = values[6];
        student.Email = values[7];
        student.Gpa = Convert.ToDouble(values[8]);

        students.Add(student);
    }
}
var noTelephone = students.Count(x => x.Telephone == "'NONE");
int noEmails = students.Count(x => x.Email == "'NONE");

Console.WriteLine("The average gpa is..." + students.Average(x => x.Gpa));
Console.WriteLine("Total last names with Anderson is..." + students.Count(x => x.LastName == "'Anderson"));
Console.WriteLine("Total number of students is..." + students.Count);
Console.WriteLine("Total with no emails is..." + noEmails);
Console.WriteLine("Total with no telephone is..." + noTelephone);
Console.WriteLine("Total with no telephone or emails are..." + (noEmails + noTelephone));
Console.ReadKey();
    }
}

You can easily perform other operations on the data afterwards and you have a nice object to work with, instead of guessing what values[7] is.

Upvotes: 1

popdan
popdan

Reputation: 471

I'm not sure what you're after but if you want to count the number of times 'NONE' occurs in the file, this should work:

while (!inFile.EndOfStream)
    {
        inValue = inFile.ReadLine()
        myCount = System.Text.RegularExpressions.Regex.Matches(inValue, "NONE").Count;

     }
    Console.WriteLine("NONE occurs {0} time(s).", myCount);

Upvotes: 1

Fung
Fung

Reputation: 3558

When you split the first line of your input file by " ":

students (LIST

You only get 2 delimited values, causing the Index out of Range exception when you try to access more values, e.g.

if (values[2] == "'Anderson")

You will have to check the number of available delimited values before you try to access them.

Upvotes: 0

Related Questions