C# - 如何传递/跳过数组中的数据

时间:2014-04-06 06:12:06

标签: c# arrays

我正在为我的编程和问题解决课程开发一个项目,这个特定的程序假设从文本文件(记事本)中读取人口普查数据,然后显示每个年龄组的居民人数以及数量每个地区的居民。

来自作业:

每条记录中的字段将以逗号分隔。

例如: 21,M,S,1

"该市有22个区。人口普查部门希望查看每个地区有多少居民的清单,以及以下每个年龄组(所有地区合并)的居民数量:18岁以下,18岁至30岁,31岁至45岁,46岁以下64岁,65岁或以上。"

我有一个文本文件要测试,但是一切似乎都有效,如果我使用的是字符而不是数字(例如" a"或" B")它会破坏我的程序。如果数据不是数字,我怎么能告诉程序跳过数据,或者我猜测" int"值?我假设某种try.Parse,但是我只看过它通过控制台输入演示,而不是来自数组......

我的代码如下。谢谢您的帮助。我感谢所有的批评和建议。

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

namespace Project2
{
class Program
{
    static void Main(string[] args)
    {            
        int[] ageData = new int[900];
        int[] districtDataA = new int[900];
        int[] ageGroup = new int[5];
        int[] districtCount = new int[22];
        int i = 0;
        foreach (string line in File.ReadAllLines("test.txt"))
        {
            string[] fields = line.Split(',');

            ageData[i] = int.Parse(fields[0]);          
            districtDataA[i] = int.Parse(fields[3]);


            if (ageData[i] > 0 && ageData[i] <= 18)
            {
                ageGroup[0] = ageGroup[0] + 1;
            }
            if (ageData[i] > 18 && ageData[i] <= 30)
            {
                ageGroup[1] = ageGroup[1] + 1;
            }
            if (ageData[i] > 30 && ageData[i] <= 45)
            {
                ageGroup[2] = ageGroup[2] + 1;
            }
            if (ageData[i] > 45 && ageData[i] <= 64)
            {
                ageGroup[3] = ageGroup[3] + 1;
            }
            if (ageData[i] >= 65)
            {
                ageGroup[4] = ageGroup[4] + 1;
            }

            //District Count info
            if (districtDataA[i] == 1)
            {
                districtCount[0] = districtCount[0] + 1;
            }
            if (districtDataA[i] == 2)
            {
                districtCount[1] = districtCount[1] + 1;
            }
            if (districtDataA[i] == 3)
            {
                districtCount[2] = districtCount[2] + 1;
            }
            if (districtDataA[i] == 4)
            {
                districtCount[3] = districtCount[3] + 1;
            }
            if (districtDataA[i] == 5)
            {
                districtCount[4] = districtCount[4] + 1;
            }
            if (districtDataA[i] == 6)
            {
                districtCount[5] = districtCount[5] + 1;
            }
            if (districtDataA[i] == 7)
            {
                districtCount[6] = districtCount[6] + 1;
            }
            if (districtDataA[i] == 8)
            {
                districtCount[7] = districtCount[7] + 1;
            }
            if (districtDataA[i] == 9)
            {
                districtCount[8] = districtCount[8] + 1;
            }
            if (districtDataA[i] == 10)
            {
                districtCount[9] = districtCount[9] + 1;
            }
            if (districtDataA[i] == 11)
            {
                districtCount[10] = districtCount[10] + 1;
            }
            if (districtDataA[i] == 12)
            {
                districtCount[11] = districtCount[11] + 1;
            }
            if (districtDataA[i] == 13)
            {
                districtCount[12] = districtCount[12] + 1;
            }
            if (districtDataA[i] == 14)
            {
                districtCount[13] = districtCount[13] + 1;
            }
            if (districtDataA[i] == 15)
            {
                districtCount[14] = districtCount[14] + 1;
            }
            if (districtDataA[i] == 16)
            {
                districtCount[15] = districtCount[15] + 1;
            }
            if (districtDataA[i] == 17)
            {
                districtCount[16] = districtCount[16] + 1;
            }
            if (districtDataA[i] == 18)
            {
                districtCount[17] = districtCount[17] + 1;
            }
            if (districtDataA[i] == 19)
            {
                districtCount[18] = districtCount[18] + 1;
            }
            if (districtDataA[i] == 20)
            {
                districtCount[19] = districtCount[19] + 1;
            }
            if (districtDataA[i] == 21)
            {
                districtCount[20] = districtCount[20] + 1;
            }
            if (districtDataA[i] == 22)
            {
                districtCount[21] = districtCount[21] + 1;
            }

            i++;

        }//End For

        Console.WriteLine("This program provides a list of residents in 5 age groups,");
        Console.WriteLine("And a list of residents in each district 1-22");
        Console.WriteLine("(1.)----------------AGE-GROUP-Count----------------");
        Console.WriteLine("Age Group 18 & under = {0}", ageGroup[0]);
        Console.WriteLine("Age Group 18-30 = {0}", ageGroup[1]);
        Console.WriteLine("Age Group 31-45 = {0}", ageGroup[2]);
        Console.WriteLine("Age Group 46-64 = {0}", ageGroup[3]);
        Console.WriteLine("Age Group 65 & over = {0}", ageGroup[4]);
        Console.WriteLine("(2.)--------------COUNT-PER-DISRTRICT--------------");
        Console.WriteLine("District 1 = {0}", districtCount[0]);
        Console.WriteLine("District 2 = {0}", districtCount[1]);
        Console.WriteLine("District 3 = {0}", districtCount[2]);
        Console.WriteLine("District 4 = {0}", districtCount[3]);
        Console.WriteLine("District 5 = {0}", districtCount[4]);
        Console.WriteLine("District 6 = {0}", districtCount[5]);
        Console.WriteLine("District 7 = {0}", districtCount[6]);
        Console.WriteLine("District 8 = {0}", districtCount[7]);
        Console.WriteLine("District 9 = {0}", districtCount[8]);
        Console.WriteLine("District 10 = {0}", districtCount[9]);
        Console.WriteLine("District 11 = {0}", districtCount[10]);
        Console.WriteLine("District 12 = {0}", districtCount[11]);
        Console.WriteLine("District 13 = {0}", districtCount[12]);
        Console.WriteLine("District 14 = {0}", districtCount[13]);
        Console.WriteLine("District 15 = {0}", districtCount[14]);
        Console.WriteLine("District 16 = {0}", districtCount[15]);
        Console.WriteLine("District 17 = {0}", districtCount[16]);
        Console.WriteLine("District 18 = {0}", districtCount[17]);
        Console.WriteLine("District 19 = {0}", districtCount[18]);
        Console.WriteLine("District 20 = {0}", districtCount[19]);
        Console.WriteLine("District 21 = {0}", districtCount[20]);
        Console.WriteLine("District 22 = {0}", districtCount[21]);


    }
}

}

2 个答案:

答案 0 :(得分:0)

试试这个:

double n;
foreach (string line in File.ReadAllLines("test.txt"))
    {
        string[] fields = line.Split(',');
        bool ageIsNumeric = double.TryParse(fields[0], out n);
        bool districtIsNumeric = double.TryParse(fields[3], out n);
        if(!ageIsNumeric || !districtIsNumeric)
            continue;
        ageData[i] = int.Parse(fields[0]);          
        districtDataA[i] = int.Parse(fields[3]);

答案 1 :(得分:0)

您从txt文件中读取数据作为字符串 在try catch块

中的foreach语句中插入代码
    foreach()
{

try{
//your code here

}catch(Exception){}
}