使用我的值索引超出范围

时间:2013-10-18 02:39:24

标签: c#

我试图打印时间'NONE出现,因为我已准确打印所有其他信息。我相信它可能是我用我的价值观做的事情,但每次我修复一些东西并让我的代码编译,我一直得到零作为我的答案。这是为什么?

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());
    }
}
}

3 个答案:

答案 0 :(得分:1)

在试图弄清楚电子邮件是否存在时,你不在身边

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();

更好的方法是将文本文件序列化为.NET对象,然后对它们执行计算。看看这个

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();
    }
}

之后您可以轻松地对数据执行其他操作,并且您有一个很好的对象可以使用,而不是猜测[7]是什么值。

答案 1 :(得分:1)

我不确定你在追求什么,但如果你想计算文件中出现'NONE'的次数,这应该有效:

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

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

答案 2 :(得分:0)

" "分割输入文件的第一行时:

students (LIST

当您尝试访问更多值时,只会获得2个分隔值,导致Index超出范围异常,例如

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

在尝试访问它们之前,您必须检查可用的分隔值的数量。