2013-04-08 5 views
2

그래서이 코드는 txt의 가상 센서스 데이터를 정렬합니다. 파일 :C#에서 범위를 벗어난 인덱스 식별

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

class Program 
{ 
    const int SIZE = 900; 
    const int SIZEDISTRICT = 22; 
    const int RANGE = 5; 
    static void Main() 
    { 
     //These arrays will hold the split data from a text file. 
     int[] districtDataD = new int[900]; 
     string[] districtDataG = new string[900]; 
     string[] districtDataM = new string[900]; 
     int[] districtDataA = new int[900]; 

     //countDistrict will hold how many hypothetical people in each hypothetical district and 
     //ages will hold how many hypothetical people between certain hypothetical ages. 
     int[] countDistrict = new int[SIZEDISTRICT]; 
     int[] ages = new int[RANGE] { 0, 18, 30, 45, 65}; 


     //Modules 
     ReadFile(districtDataD, districtDataG, districtDataM,districtDataA); 
     CountPopulation(districtDataD, countDistrict); 
     AgeRanges(districtDataA, ages); 
     DisplayData(districtDataD, districtDataA, ages); 
    }//End Main 


    //This module splits and inserts the data into the four first arrays 
    static void ReadFile(int[] districtDataD, string[] districtDataG, string[] districtDataM, int[] districtDataA) 
    { 
     string[] lines = File.ReadAllLines("census.txt"); 
     int i = 0; 

     while (i < SIZE && i < districtDataD.Length) 
     { 
      foreach (string line in File.ReadAllLines("census.txt")) 
      { 
       string[] parts = line.Split(','); 

       districtDataD[i] = int.Parse(parts[0]); 
       districtDataG[i] = parts[1]; 
       districtDataM[i] = parts[2]; 
       districtDataA[i] = int.Parse(parts[3]); 
       i++; 
      } 
     } 
    } 
    //This module counts how many hypothetical people are in each fictional district 
    static void CountPopulation(int[] districtDataD, int[] countDistrict) 
    { 
     int i = 0; 
     for (i = 0; i < districtDataD.Length; i++) 
     { 
      if (districtDataD[i] > 0 && districtDataD[i] < districtDataD.Length) 
      { 
       countDistrict[districtDataD[i]] 
        ++; 
      } 
     } 
    } 


    //This module sorts the ages into 0-18, 19-30, 31-45, 46-65, and 65 and up 
    private static void AgeRanges(int[] districtDataA, int[] ages) 
    { 
     int idx = 0; 
     for (idx = 0; idx < districtDataA.Length && ages[idx] > districtDataA[idx]; idx++) 
     { 

      ages[idx] = districtDataA[idx]; 
     } 
    } 


    //This module displays the data 
    static void DisplayData(int[] countDistrict, int[] ageDistrict, int[] ages) 
    { 
     int index = 0; 
     for (index = 0; index < countDistrict.Length; index++) 
     { 
      Console.WriteLine(" District {0}: {1}", index + 1, countDistrict[index]); 
     } 

     int x = 0; 
     for (x = 0; x < ageDistrict.Length; x++) 
     { 
      Console.WriteLine("Ages under {0} : {1}", ages[x], ageDistrict[x]); 
     } 
    } 
} 

색인이 범위를 벗어나 오류가 발생하지만 어디에서 어떻게 찾을 수 있는지 알 수 없습니다.

txt. 파일은 현재 이와 같이 보이지만 약 100 개 정도를 포함하도록 확장됩니다. 900이 상한으로 사용됩니다.

21, F, S, 14

41, F, m은 8

29 s가, F 22

12, m은, (S12)

11 m, m, 4

6 m, S (12)

9, F, S, 2

30, f, s, 1

+1

당신이 다음 배열을 사용하지 않는 저장해야 얼마나 많은 요소 모르는 경우; 컬렉션 대신'List'를 사용하십시오. –

+0

어떤 메서드와 코드 줄 번호가 오류를 발생 시켰는지 알려주는 예외와 함께 스택 추적을 얻어야합니다. 또한 예외가 발생할 때마다 디버깅 및 중지를 시도하십시오. –

+0

각 루프에 추가되는 SIZE에 변수를 사용하는 것은 어떻습니까? – Sabotenderizer

답변

1

배열의 길이를 알아 내지 않으면 대신 모음을 사용하십시오. 이렇게하면 파일 크기와 길이가 독립적이됩니다. 배열을 사용하면 배열에 따라 파일의 길이에 따라 변경할 수있는 배열 길이를 설정해야합니다. 적절한 구현을하지 않은 경우 인덱스가 바뀌면 항상 어려움이 있습니다. 에

districtDataD.Add(1); 
+0

나는 아직 거기에 있지 않다. – Sabotenderizer

+0

@Sabotenderizer 당신은 무엇을 의미합니까? – Alex

1

대부분의 아마 당신이 점점 오류 :

  string[] parts = line.Split(','); 
      districtDataD[i] = int.Parse(parts[0]); 
      districtDataG[i] = parts[1]; 
      districtDataM[i] = parts[2]; 
      districtDataA[i] = int.Parse(parts[3]); 

확인 모음을 사용하면이

List<int> districtDataD = new List<int>(); 

은 그럼 그냥 같이이 컬렉션에 항목을 추가 할 수 이러한 문제가의 자신을 피하기 parts.Length을 사용하십시오. 귀하의 회선 중 3 개의 쉼표 (',')가 포함되지 않을 수 있습니다.

오류의 또 다른 가능한 소스는 다음과 같습니다

countDistrict[districtDataD[i]] 
+0

지금은 쉼표가 3 개 미만이거나 쉼표가 없습니다. – Sabotenderizer

+0

나는 countDistrict [districtDataD [i]]라고 생각한다. ++; 불완전한 문장이다. 만약 당신이 완료하시기 바랍니다 수 ??? –

+0

다른 프로그램을 사용하기 전에 시도해 보았습니다. – Sabotenderizer

관련 문제