2013-05-15 26 views
0

這裏是我的代碼:無法拆分字符串,並混淆「;」要求

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

namespace DataAnalysis 
{ 
class Data 
{ 
    public int InTime; 
    public string InLocation; 
    public bool Direction; 
    public int LOS_F; 

    // create a Data object from a CSV format string. 
    static Data FromString(string line) 
    { 
     var fields = line.split(","); 
     return new Data 
     { 
      InTime = TimeSpan.Parse(fields[3]), 
      InLocation = fields[5], 
      Direction = fields[5][0], // to get the direction E/N/S/W 
      LOS_F = float.Parse(fields[16]) 
     }; 
    } 
} 

class Program 
{ 
    string[] directions = new string[] { "E", "N", "S", "W" }; 

    static void Main(string[] args) 
    { 
     var path = @"C:\Documents and Settings\Siva-Admin\Desktop\5.5 Capacity Models\"; 
     //   ^--- No need to escape the backslashes 

     var subdirs = Directory.GetDirectories(path); 
     // The subdirs variable contains the FULL paths 

     foreach (string subdir in subdirs) 
     { 
      List<List<float>> allAvgs = new List<List<float>>(); 

      using (StreamWriter compiled = new StreamWriter(
        Path.Combine(subdir, "compiledresults.csv"))) 
      { 
       compiled.Write("heading,EastAvg,NorthAvg,SouthAvg,WestAvg"); 

       for (int i = 1; i <= 10; i++) 
       { 
        List<Data> info = new List<Data>(); 
        using (StreamReader reader = new StreamReader(
          Path.Combine(subdir, "results" + i.ToString() + @"\JourneyTimes.csv"))) 
        { 
         // Read the header line first! 
         string line = reader.ReadLine(); 

         while ((line = reader.ReadLine()) != null) 
          info.Add(Data.FromString(line)); 
        } 

        List<float> avgs = new List<float>(); 

        for (string dir in directions) 
        { 
         List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>; 
         float sum = perDirection.Sum(d => d.LOS_F); 
         float average = sum/perDirection.Count(); 
         avgs.Add(average); 
        } 

        allAvgs.Add(avgs); 

        compiled.Write("results" + i.ToString() + "," + string.Join(",", avgs) + "\n"); 
       } 

       compiled.Write("scenario_average"); 

       for (int j = 1; j <= 4; j++) 
       { 
        compiled.Write("," + allAvgs.Sum(d => d[0])/allAvgs.Count()); 
       } 
      } 
     } 
    } 
} 

}

我收到以下錯誤:

Error 1; expected(Line 67, "for (string dir in directions)") 
Error 2; expected(LINE 67, "       ")  
Error 3 'string' does not contain a definition for 'split' and no extension method 'split' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) (LINE 20, var fields = line.split (",");) 

視爲合格這些論點我不明白的;的需求是他們不?我也不明白爲什麼我不能分割一個字符串。

+3

使用'line.split(',');'代替 – Mehran

+0

產生相同的錯誤!字符串不包含定義... – user2379217

+0

標準庫中的公共方法名稱**始終**以大寫字母開頭,因此它是「分割」。 – RoadieRich

回答

6

錯誤1和2:您的迴路應foreach循環:

foreach (string dir in directions) 

錯誤3:在S中分裂應該是上殼體:

var fields = line.Split(','); 
+0

聰明的人。它是大寫的 – TreantBG

5

for (string dir in directions)應該foreach (string dir in directions)

編輯補充:

Addtionally,在這裏:

List<Data> perDirection = info.Where(d => d.Direction = dir) as List<Data>; 

你似乎是在進行賦值,而不是一個平等的檢查,即

d.Direction = dir應該是d.Direction == dir

但是,它仍然不起作用,因爲您正在比較字符串和布爾值。

+1

我很懷疑'Where List '將在Where方法結束時起作用。相反,在末尾添加.ToList()可能會更容易。 –

1

您需要將line.split(",")替換爲line.Split(',')(注意大寫S)。
(也可以用其他人指出的第67行替換forforeach)。

1

ERROR1 & Error2使用foreach而不是for;

Erorr3它是拆分而不是拆分。