ArgumentIndexOutOfBounds异常string.Length

时间:2015-06-03 14:52:24

标签: c# loops file-io exception-handling

以下是我过去几天遇到的问题。这个问题与我昨天提出的问题非常相似,但是当我更新问题时没有人触及它,所以我在一个新问题中再次提出这个问题。

我正在开发一个程序,它接受一个大文本文件,将其分成两组:a和b,然后读取每行数据。一行数据将如下所示:

5/22/2015 12:15:55 AM | Batch 8|429/529|81.10 %|BLV-R|Processed VLZYYL...Checking refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $100.00. Actual Savings: $1,780.60. Savings found: $1,780.60 (31.11 %). Remark written. UDID written. Queued to 0TA9/208*11. Queued to 0TA9/161*222.

我不会解释一切意味着什么。但该计划正在关注的关键因素是"储蓄发现:"以及它旁边的百分比。然而,并不是每条线路都有节省,实际上大多数线路没有节省,但其中一些线路没有。下面我有一段代码,告诉我文件名是什么,文件有多少行,查看一组中的每一行,写出处理了多少PNRS,有多少PNRS可以节省,是什么所有节省的PNRS的百分比(百分比四舍五入到3位小数)然后它应该写出每行都有"存储找到:"如果百分比是30%或更高,它将在该行的前面放置一个星号。如果节省30%或更多并节省500美元或更多,那么它将在该行的前面放置两个星号。下面我有一个示例,说明总体输出应该是什么以及我到目前为止的代码:

Console Idea:
FIle Name: PNRS
Total Lines: 123,123

Group: A
Total PNRS: 123
PNRS With Savings: 23
% With Savings: 10%

* 5/21/2015 11:55:56 PM | Batch 6|386/767|50.33 %|CH2M-R|Processed          NXRMN5...Checking refundable and non-refundable fares. Traditional Booking. Inside ticketing window. Minimum Savings Required: $131.00. Actual Savings: $257.18. Savings found: $257.18 (11.55 %). Savings were previously found.
2015-234wefsaf LINE GOES HERE** 2015    LINES 
Group: B SAME FORMAT AS GROUP A

我一直致力于的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Linq;
namespace DanProject
{
class Program_Origional
{
    static void Main(string[] args)
    {
        //the following series of code is phase1
            try
        {


            string path = @"c:\users\povermyer\documents\visual studio 2013\Projects\DanProject\PNRS\PNRS.log";

            if (!File.Exists(path))
            {

                //Console.WriteLine("File Not Found");
                throw new FileNotFoundException();
            }
            else
            {


                string[] lines = System.IO.File.ReadAllLines(path);
                var count = File.ReadLines(path).Count();
                List<string> a = lines.Take(7678).ToList();
                List<string> s = lines.Skip(7678).Take(5292).ToList();


                Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(path));
                Console.WriteLine(lines.Count());


                if (a.Any(item => item.Contains("Savings found")))
                {
                    Console.WriteLine("\nGroup a\n");
                    var aPNRSThatWereProcessed = a.Count(line => line.Contains("Processed"));

                    Console.WriteLine("There were " + aPNRSThatWereProcessed + " PNRS that were processed in a");


                    //code to find number of savings

                    int aPNRSWhereSavingsWereFound = a.Count(line => line.Contains("Savings found:"));
                    Console.WriteLine("In a, there were " + aPNRSWhereSavingsWereFound + " PNRS that had savings");

                    //code to find percentage for a
                    decimal aPercentage = ((decimal)aPNRSWhereSavingsWereFound / aPNRSThatWereProcessed *100);
                    decimal result1 = Math.Round(aPercentage, 3);
                    Console.WriteLine("The percentage of PNRS in a that had savings were " + result1 + "%");

                    string find = "Savings found:";
                    foreach (var line in a.Where(w => w.Contains(find)))
                    {
                        var subStr = line.Substring(0, line.IndexOf(find) + find.Length);
                        var startIndex = subStr.IndexOf('(');
                        var endIndex = subStr.IndexOf(')');

                        var savings = double.Parse(subStr.Substring(2, startIndex - 1).Trim());
                        var percent = double.Parse(subStr.Substring(startIndex + 1, endIndex - startIndex - 2).Trim());

                        Console.WriteLine("{0}{1}{2}", (percent >= 30) ? "*" : string.Empty,

                                                       (percent >= 30 && savings >= 500) ? "*" : string.Empty,
                                                        line);
                    }
                }








            }
        }

        catch (Exception ex)
        {

            Console.WriteLine(ex.ToString());
        }
        finally
        {
            Console.ReadKey();
        }


    }
}

}

我的问题在于foreach循环。在foreach循环之上的所有东西都是完美的,然后当我编译它时,我收到这个确切的错误消息:     System.ArgumentOutOfRangeException:Length不能小于零。参数名称:长度     在System.String.Substring(Int32,startIndex,Int32,length)     at(我正在使用的文本文件的文件名很长):第66行

正如我所看到的,问题在于.Length语句,但是当我尝试使用()修复它时,VS对我大吼大叫并说我无法做到。它说&#34;非可调用成员&#39; string.Length&#39;不能像方法一样使用&#34;。

既然我已尽可能详细,请帮助我弄清楚如何摆脱这个问题并让这个控制台应用程序滚动!

1 个答案:

答案 0 :(得分:0)

然后@ steve16351告诉你问题是什么。

你正在做var subStr = line.Substring(0, line.IndexOf(find) + find.Length);

将返回'Savings found'

你想要像

这样的东西
var subStr = line.Substring(line.IndexOf(find) + find.Length);

在“发现储蓄”之后会给你一切

告诉你这将是一个多哈时刻。 :)