gpt4 book ai didi

C# - System.FormatException 类型的未处理异常 - 列出字符串以列出 int

转载 作者:太空狗 更新时间:2023-10-29 23:29:48 25 4
gpt4 key购买 nike

我收到错误“FormatException 未处理”- 关于此:

           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

我只是想修改小时和分钟的数字。计算耗时(程序打开的时间)并将其添加到已存储在文本文件中的时间。然后将其保存回文件而不是将其输出到控制台。 (我解析文件并使用正则表达式来获取数字,我认为我需要将 if 语句转换为分钟 >60 类似于此 afaik):

                    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;
}

最佳答案

这是因为您忘记检查您的正则表达式匹配是否成功。

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

实际上,当匹配失败时,match.Groups["hours"].Value 只会返回一个空字符串。随后 Int32.Parse(String.Empty) 将抛出一个 FormatException

一个简单的测试:

 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

关于C# - System.FormatException 类型的未处理异常 - 列出字符串以列出 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34176465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com