2015-12-09 14 views
5

私は「FormatExceptionが未処理だった」エラーを取得していますリストを表示するリスト文字列 - この上:のC# - 型にSystem.FormatExceptionの未処理の例外 - int型

  List<int> myStringList = LoadHours.ConvertAll(s => Int32.Parse(s)); 

を私は追加を行うことができるようにする必要がありますし、私はそれらを整数に変換しようとしているのです。だから私はこれを処理する方法がわかりません、文字列に変換する必要がありますか?

コード:

  //string txt = "Account: xxx123xxx\r\nAppID 10: 55 Hours 4 Minutes\r\n\r\nAccount: xxx124xxx\r\nAppID 730: 62 Hours 46 Minutes\r\nAppID 10: 3 Hours 11 Minutes\r\n\r\nAccount: xxx125xxx\r\nAppID 10: 0 Hours 31 Minutes\r\n"; 
      string path = @"Hours.txt"; 
      string text = File.ReadAllText(@"Hours.txt"); 
      Regex pattern = new Regex(@"^((AppID (?<appid>\d+): ((?<hours>\d+) Hours)?((?<minutes>\d+) Minutes)?)|(Account: (?<account>xxx\d+xxx)))", RegexOptions.Multiline); 

      string[] lines = File.ReadAllLines(path); 

      int HrElapsed; 
      int MinElapsed; 

      Int32.TryParse(HoursElapsed(), out HrElapsed); 
      Int32.TryParse(MinutesElapsed(), out MinElapsed); 

      List <string> LoadHours = new List <string>(); 
      List <string> LoadMinutes = new List <string>(); 

      // Iterate through lines 
      foreach(string line in lines) { 
      //Check if line matches your format here 
      Match match = pattern.Match(line); 
      LoadHours.Add(match.Groups["hours"].Value); 
      LoadMinutes.Add(match.Groups["minutes"].Value); 
      //File.WriteAllText(@"Hours.txt", Regex.Replace(File.ReadAllText(@"Hours.txt"), @"AppID \d+:\s(\d+\s\w+\s\d+\s\w+)",LogTime)); 
      } 
      List <int> myStringList = LoadHours.ConvertAll(s => Int32.Parse(s)); 
      List <int> myStringList2 = LoadMinutes.ConvertAll(s => Int32.Parse(s)); 

      for (int i = 0; i < myStringList.Count; ++i) 

      { 
      myStringList[i] = myStringList[i] + HrElapsed; 
      } 

      for (int i = 0; i < myStringList2.Count; ++i) { 
      myStringList2[i] = myStringList2[i] + MinElapsed; 
      } 

      string[] the_array = myStringList.Select(i => i.ToString()).ToArray(); 
      string[] the_array2 = myStringList2.Select(i => i.ToString()).ToArray(); 

      Regex re = new Regex(@"^((AppID (?<appid>\d+): ((?<hours>\d+) Hours)?((?<minutes>\d+) Minutes)?)|(Account: (?<account>xxx\d+xxx)))", RegexOptions.Multiline); 
      string hackount = ""; 
      (from m in re.Matches(text).Cast <Match>() 

      let acc = m.Groups["account"].Value 
      let app = m.Groups["appid"].Value 
      // let hrs = m.Groups["hours"].Value 
      // let mins = m.Groups["minutes"].Value 
      let timHours = the_array 
      let timMinutes = the_array2 
      let obj = new { 
      Account = acc == "" ? hackount : (hackount = acc), AppId = app, Time = the_array, the_array2 
      } 
      where app != "" 
      select obj 

      ).ToList().ForEach(Console.WriteLine); 

Hours.txtファイルには、これを含まれています:

Account: xxx123xxx 
AppID 10: 1 Hours 5 Minutes 

Account: xxx124xxx 
AppID 10: 2 Hours 6 Minutes 
AppID 10: 3 Hours 7 Minutes 

Account: xxx125xxx 
AppID 10: 4 Hours 8 Minutes 

私はちょうど時間と分の数字を変更しようとしています。経過時間(プログラムが開いている時間)を計算し、テキストファイルにすでに格納されている量に加算します。それをコンソールに出力するのではなく、ファイルに保存し直してください。 (私は、ファイルを解析し、数字をつかむために正規表現を使用して、この私の知る限りに似た声明分> 60変換場合、私は私が行う必要があると思います):あなたの正規表現のマッチが成功するかどうかをチェックするのを忘れたためです

    int TotalHours = HrElapsed + HrStored; 
        int TotalMinutes = MinElapsed + MinStored; 

        // Just making sure the minutes don't end up as "141 minutes" so it's incrementing hours. 
        if (TotalMinutes >= 60) 
        { 
         int remainder = TotalMinutes % 60; 
         int whole = TotalMinutes/60; 
         TotalMinutes = remainder; 
         TotalHours = TotalHours + whole; 
        } 
+0

私の答えをチェックし、あなたに役立つかどうかを教えてください。 – mybirthname

答えて

2

。マッチが失敗したとき

if (match.Success) // You need this check 
{ 
    LoadHours.Add(match.Groups["hours"].Value); 
    LoadMinutes.Add(match.Groups["minutes"].Value); 
} 

実はmatch.Groups["hours"].Valueはちょうど空の文字列を返します。あなたは問題があなたが整数として無効な文字列を解析していることがわかりのように

Regex pattern = new Regex(@"(?<hours>\d+)d"); 
Match match = pattern.Match("Textwithoutdigits"); 
var value = match.Groups["hours"].Value; 
Console.WriteLine(value == String.Empty); // <-- Will print true 
+0

正しい私が真の持っている 真 真 真 真 真 真 真 真 真 – Kai

+0

私は、この出力がコンソールに得るチェック「{アカウント=、APPID = 10、時間=システムを追加する場合。 String []、the_array2 = System.String []} " – Kai

+0

Thxが動作しました。 – Kai

1

:そしてその後Int32.Parse(String.Empty)FormatException

簡単なテストをスローします。 int.TryParseメソッドを使用する必要があります。

このようにコードを書く必要があります。

private static int TryParseString(string p) 
    { 
     int result = 0; 

     bool valid = int.TryParse(p, out result); 

     if (!valid) 
      return -1; 

     return result; 
    } 
ここ

無効な文字列と置く値-1彼らのために排除されます:

List<int> myStringList = LoadHours.ConvertAll(s => TryParseString(s)); 

その後、あなたはメソッドを作成する必要があります。その後、この-1の値を削除することができます。

myStringList = myStringList.Where(x=>x!=-1).ToList(); 
+0

それは助けました!どうも! – Kai

+0

あなたは歓迎です – mybirthname

関連する問題