无法移动到下一行,从文本文件中读取 [英] Unable to move to next line, reading from text file

查看:123
本文介绍了无法移动到下一行,从文本文件中读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述








指数超出阵列范围 - 错误





 List< string> Messagetype =  new  List< string>(); 

使用 var reader = new StreamReader( @ C:\ Users \Documents\TRWork \Download History \\ \\ unpack\Estxxx.Acxx.AMER-EARN-2002-2003.1.2016-07-16-0001.Full.1.of.1.txt))
{
< span class =code-keyword> string 行;
while ((line = reader.ReadLine())!= null
{
// 拆分行
字符串 [] parts = line.Split( new [] { < span class =code-string> |},StringSplitOptions.None);

// 获取有效文本
Messagetype.Add(parts [ 1 ]);
Console.WriteLine(Messagetype.Count());

}
}





我的尝试:



我试过调试应用断点



我无法移动到下一行。第一行被读取,字符串值存储在列表变量Messagetype中。然而,在第一次循环之后,当它迭代时,它不会移动到下一行并变为null

解决方案

这与移动到下一行 - 它也与您阅读的数据有关。

  string  [] parts = line。拆分( new  [] {  |},StringSplitOptions.None); 
Messagetype.Add(部件[ 1 ]);



当你读的行没有包含任何|然后,Split方法返回一个只包含一个元素的数组。因此,当您尝试访问索引1处的元素时,它不存在并且您收到错误。

首先检查您的文本文件,看看为什么它有一行不有任何|字符。

如果需要,请在循环内检查:

  string  [] parts = line.Split( new  [] {  |},StringSplitOptions.None); 
if (parts.Length > 1
{
Messagetype.Add(parts [ 1 ]);
}


你应该确实学会尽快使用调试器。现在是时候看到你的代码正在执行并确保它能达到你的预期。



调试器允许你逐行跟踪执行,检查变量你会看到有一点让它停止做你期望的事情。

调试器 - 维基百科,免费的百科全书 [ ^ ]

< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn>在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]



您可能会看到错误发生在

 Messagetype.Add(parts [ 1 ]); 



部分[1] 不要因为你而存在拆分|,这不在


Hi


Index was outside the bounds of the array - Error


 List<string> Messagetype = new List<string>();

using (var reader = new StreamReader(@"C:\Users\Documents\TRWork\Download History\unpack\Estxxx.Acxx.AMER-EARN-2002-2003.1.2016-07-16-0001.Full.1.of.1.txt"))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        //split the line
                        string[] parts = line.Split(new[] { "|" }, StringSplitOptions.None);

                        //get valid text
                        Messagetype.Add(parts[1]);
                        Console.WriteLine(Messagetype.Count()); 
                       
                    }
                }



What I have tried:

I tried Debugging applying breakpoint

I am unable to move to the next line. First line gets read and the string value is stored in the list variable "Messagetype". However after the first loop from while next time when it iterates it does not move to the next line and becomes null

解决方案

This has nothing to do with "moving to the next line" - it's too do with the data you read.

string[] parts = line.Split(new[] { "|" }, StringSplitOptions.None);
Messagetype.Add(parts[1]);


When the line you read does not contain any "|" characters then the Split method returns an array containing only one element. So when you try to access the element at index one, it doesn't exist and you get an error.
So start by checking your text file, and see why it has a line which doesn't have any "|" characters.
If that is expected, then check it inside your loop:

string[] parts = line.Split(new[] { "|" }, StringSplitOptions.None);
if (parts.Length > 1)
   {
   Messagetype.Add(parts[1]);
   }


You should really learn to use the debugger as soon as possible. It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

You will probably see that the error is at

Messagetype.Add(parts[1]);


and that parts[1] do not exist because you split at "|", which is not in line.


这篇关于无法移动到下一行,从文本文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆