2013-04-08 2 views
0

다음을 포함하는 데이터 목록을 정렬하려고합니다 :입력이 어딘가에서 사라집니다 (0을 인쇄 중)

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

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(countDistrict, 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 < 0) 
     { 
      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) 
      { 
       districtDataD[countDistrict[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[] districtDataA, 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 < ages.Length; x++) 
     { 
      Console.WriteLine("Ages over {0} : {1}", ages[x], districtDataA[x]); 
     } 
    } 
} 

전류 출력은 다음과 같습니다 : 내 데이터가

A bunch of zeroes

잃어버린입니까?

+0

디버깅을 시도 했습니까? –

+4

'ReadFile' 메쏘드에서이 라인이''(i codingbadger

+0

Visual Studio에 이것을 빠르게 연결하면 위의 @Barry를 볼 수 있습니다. 실제로 맞습니다. – Arran

답변

0

문제는이 줄에 있습니다.

while (i < SIZE && i < 0) 

이 루프는 실행되지 않습니다. 이것으로 교체하십시오.

while (i < SIZE && i <= 0) 
{ 
    foreach (string line in File.ReadAllLines("census.txt")) 
    { 
     if (!string.IsNullOrEmpty(line)) 
     { 
      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++; 
     } 
    } 
} 
+0

이로 인해 인덱스가 범위를 벗어납니다. – Sabotenderizer

+0

코드에 다른 문제가 있습니다. –

+0

나를 보여줄 수 있습니까? – Sabotenderizer

0

ReadFile이 잘못되었습니다. 대신이 같은 것을 시도해보십시오.

static void ReadFile(int[] districtDataD, string[] districtDataG, string[] districtDataM, int[] districtDataA) 
{ 
    // Read in the file contents 
    string[] lines = File.ReadAllLines("census.txt"); 
    int i =0; 
    //iterate through each line 
    foreach(var line in lines) 
    { 
     //split the line by a comma 
     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++; 
    } 

} 

현재 파일에 읽고 다음 다시 파일을 읽고 line에 값을 할당 lines에 할당된다. foreach을 사용하는 경우 현재 어떤 행을 추적하지 않고도 각 행을 쉽게 반복 할 수 있습니다.

관련 문제