2012-01-15 13 views
1

私は(非常に)C#で新しく、.txtファイルから文字列のリストを取り込み、表示する最も一般的なものの上位20個を作成しようとしています。これはプロジェクトのためのものですが、私はうそをつくつもりはありませんが、私がコードの特定の部分に到達できない理由を理解できず、助けに感謝します。到達不能なコードと検証

これまでの私のコードは次のとおりです。

// I have used framework 4 
public class Program 
{ 

    private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string, int> kv2) 
    { 
     return kv2.Value == kv1.Value ? kv1.Key.CompareTo(kv2.Key) : kv2.Value - kv1.Value; 
    } 

    public static void Main() 
    { 
     { 
      try 
      { 
       Dictionary<string, int> histogram = new Dictionary<string, int>();  // creates a dictionary from the text file 
       using (StreamReader reader = new StreamReader("tweets.txt"))  //reads the text file specified 
       { 
        string difline; 
        while ((difline = reader.ReadLine()) != null)  //continues until no lines left 
        { 
         if (histogram.ContainsKey(difline))  //counts specific strings 
          ++histogram[difline]; 
         else 
          histogram.Add(difline, 1); 
        } 
       } 


       using (StreamReader sr = new StreamReader("tweets.txt"))  // Create an instance of StreamReader to read from a file. 
       // also closes the StreamReader. 
       { 
        string line; 
        long linecount = linesinfile("tweets.txt"); 
        Console.WriteLine(linecount); 
        TextWriter tw = new StreamWriter("tweets.html"); //create a writer 
        tw.WriteLine("<html> <body>"); 
        while ((line = sr.ReadLine()) != null) // Read and display lines from the file until the end of the file is reached. 
        { 
         Console.WriteLine(line); 
         tw.WriteLine("{0} <br />", line); //write the lines of text to the html file 

        } 
        tw.WriteLine("</html> </body>"); 
        tw.Close(); //close the writer 
       } 

      } 

      catch (Exception e) 
      { 

       Console.WriteLine("The file could not be read:"); // Let the user know what went wrong. 
       Console.WriteLine(e.Message); 
      } 
      Console.Write("\nPress any key to continue . . . "); 
      Console.ReadKey(true); 
     } 
    } 


    static long linesinfile(string l) 
    { 
     long count = 0; 
     using (StreamReader r = new StreamReader(l)) 
     { 
      string line; 
      while ((line = r.ReadLine()) != null)  //counts lines until last line 
      { 
       if (line.StartsWith("#") & line.Length > 1)  //only count lines which start with # 
       { 
        count++; //increases the count 
       } 
      } 
      return count;  //displays the line count 
     } 

     //this code is unreachable 

     Dictionary<string, int> histogram = new Dictionary<string, int>(); 
     using (StreamReader reader = new StreamReader("tweets.txt")) 
     { 
      string line; 

      while ((line = reader.ReadLine()) != null) 
      { 
       if (histogram.ContainsKey(line)) 
        ++histogram[line]; 
       else 
        histogram.Add(line, 1); 
      } 
      { 
       Console.Write("{0} ", histogram); 
      } 
     } 

     List<KeyValuePair<string, int>> sortedHistogram = new List<KeyValuePair<string, int>>(histogram); 
     sortedHistogram.Sort(Compare); 
     foreach (KeyValuePair<string, int> kv in sortedHistogram) 
      Console.WriteLine("{0}\t{1}", kv.Value, kv.Key); 
    } 
} 
+4

あなたのコードに到達していないポイントがわかりやすいと思うでしょうか? – Patrick87

+4

@ Patrick87 "** //このコードは到達不可能です**" "行、私は推測していますか? – hvd

+0

私はこの行に到達できないと言っている行にコメントしました** – user1150442

答えて

3

return文は、現在のメソッドの実行を停止し、呼び出し側に制御を返します。あなたの場合、あなたのlinesinfileメソッドで、早過ぎるreturn count;声明があるようです。 return count;を、の直前のlinesinfileメソッドのの末尾に移動してみてください。これが機能するはずです。あなたは無条件return countを行うあなたの機能で

+0

ダグラスありがとうございます。私はあなたが示唆したように行を移動しましたが、コードはまだ私が望む結果を生成しません。 「トップ20」の返品はまだありません – user1150442

+0

下記の私のポストを参照してください。 – Douglas

2
static long linesinfile(string l) 
     { 
      long count = 0; 
      using (StreamReader r = new StreamReader(l)) 
      { 
       string line; 
       while ((line = r.ReadLine()) != null)  //counts lines until last line 
       { 
        if (line.StartsWith("#") & line.Length > 1)  //only count lines which start with # 
        { 
         count++; //increases the count 
        } 
       } 
       return count;  //displays the line count 
      } 

ので、この関数のコードの残りの部分は、通常到達不能です。

3

このコード

return count;  //displays the line count 

は順番に、それはメソッドの実行を終了...これが到達不能であること、そのメソッド内のコードの残りの部分につながることを意味して無条件に実行されます。

1

コードには多くの冗長性があります。私はそれをトリミングし、コンソールに上位20の結果を表示しようとしました(HTMLファイル出力を排除します)。

// I have used framework 4 

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

public class Program 
{ 
    private static int Compare(KeyValuePair<string, int> kv1, KeyValuePair<string, int> kv2) 
    { 
     return kv2.Value.CompareTo(kv1.Value); // descending order 
    } 

    public static void Main() 
    { 
     try 
     { 
      int linecount = 0; 
      Dictionary<string, int> histogram = new Dictionary<string, int>();  // creates a dictionary from the text file 
      using (StreamReader reader = new StreamReader("tweets.txt"))  //reads the text file specified 
      { 
       string difline; 
       while ((difline = reader.ReadLine()) != null)  //continues until no lines left 
       {     
        linecount++; //increases the count 

        if (histogram.ContainsKey(difline))  //counts specific strings 
         ++histogram[difline]; 
        else 
         histogram.Add(difline, 1); 
       } 
      } 

      Console.WriteLine("Line count: " + linecount); 

      Console.WriteLine("Top 20:"); 
      List<KeyValuePair<string, int>> sortedHistogram = new List<KeyValuePair<string, int>>(histogram); 
      sortedHistogram.Sort(Compare); 
      foreach (KeyValuePair<string, int> kv in sortedHistogram.Take(20)) 
       Console.WriteLine("{0}\t{1}", kv.Value, kv.Key); 
     } 

     catch (Exception e) 
     { 

      Console.WriteLine("The file could not be read:"); // Let the user know what went wrong. 
      Console.WriteLine(e.Message); 
     } 

     Console.Write("\nPress any key to continue . . . "); 
     Console.ReadKey(true); 
    } 
} 
+0

冗長性のビット、それは半分笑いです。どうもありがとう、私は今、ワインボトルを置くことができます。 – user1150442

関連する問題