C# 无法获取C中数组的和#
我编写了下面的代码来读取包含数字数组的文件,然后添加它们并显示结果。但是,我收到了链接中提供的错误消息。原因是什么C# 无法获取C中数组的和#,c#,arrays,file,C#,Arrays,File,我编写了下面的代码来读取包含数字数组的文件,然后添加它们并显示结果。但是,我收到了链接中提供的错误消息。原因是什么 using System; using System.Globalization; using System.IO; using System.Linq; using System.Collections.Generic; using System.Xml.Schema; class ReadFromFile { public static void Main() {
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Xml.Schema;
class ReadFromFile
{
public static void Main()
{
StreamReader rea=new StreamReader(@"c:\users\user\desktop\try.txt");
var lineCount =File.ReadLines(@"c:\users\user\desktop\try.txt").Count();
int count=(int) lineCount;
List<int> ad = new List<int>();
for (int ind = 0; ind < count; ind++)
{
Console.WriteLine(rea.ReadLine());
ad.Add(Int32.Parse(rea.ReadLine()));
}
int[] num = ad.ToArray();
int summ=num.Sum();
rea.Close();
Console.WriteLine(summ);
Console.ReadKey();
}
}
使用系统;
利用制度全球化;
使用System.IO;
使用System.Linq;
使用System.Collections.Generic;
使用System.Xml.Schema;
类ReadFromFile
{
公共静态void Main()
{
StreamReader rea=新的StreamReader(@“c:\users\user\desktop\try.txt”);
var lineCount=File.ReadLines(@“c:\users\user\desktop\try.txt”).Count();
整数计数=(整数)行计数;
列表ad=新列表();
对于(int ind=0;ind
您的错误来自以下几行:
Console.WriteLine(rea.ReadLine());
ad.Add(Int32.Parse(rea.ReadLine()));
发生的事情是,您正在获得文件中的行数计数。假设你的文件有10行。当循环到达最后一行时,在Console.WriteLine(rea.ReadLine())中调用ReadLine
方法代码>,它返回最后一行。但随后在ad.Add(Int32.Parse(rea.Readline())中再次调用Readline
方法
返回null,因为已到达文件末尾(EOF),而Int32.Parse
抛出异常,因为它无法将null转换为数字
这是来自aboutReadLine
方法:
输入流的下一行,如果到达输入流的末尾,则为null
因此,不仅您得到了例外,而且您的总和也将被取消,因为您的ad
列表将只包含文件中一半的数字。因此,如果您的文件有10行,它将只有5个数字
修复
如果文件中有数字,并且您确定它们是数字,则可以使用Int32.Parse
。如果您不确定,则使用Int32.TryParse
进行尝试,如果失败,则返回false。如果您只需要求和而不需要其他任何东西,您可以在一行代码中完成,如下所示:
File.ReadLines("PathOfYourFile").Sum(line => int.Parse(line));
File.ReadLines("PathOfYourFile").Sum(line =>
{
int possibleNumber;
if (int.TryParse(line, out possibleNumber)
{
// success so return the number
return possibleNumber;
}
else
{
// Maybe log the line number or throw the exception to
// stop processing the file or
// return a zero and continue processing the file
return 0;
}
}
如果您不确定,并且文件可能包含非数字数据,请执行以下操作:
File.ReadLines("PathOfYourFile").Sum(line => int.Parse(line));
File.ReadLines("PathOfYourFile").Sum(line =>
{
int possibleNumber;
if (int.TryParse(line, out possibleNumber)
{
// success so return the number
return possibleNumber;
}
else
{
// Maybe log the line number or throw the exception to
// stop processing the file or
// return a zero and continue processing the file
return 0;
}
}
最后,如果您想按自己的方式执行,请删除这一行,如果您的文件只有数字,则一切都应该正常:
// remove this so you do not read the line
Console.WriteLine(rea.ReadLine());
1) 您得到这个错误是因为您使用了Parse方法。当编译器无法将输入强制转换为Int32时,Parse方法会引发异常
使用TryParse,如果强制转换正常,则返回true,否则返回false:
string[] lines = System.IO.File.ReadAllLines(@"c:\try.txt");
int number;
int sum = lines.Sum(line => Int32.TryParse(line, out number) ? number : 0);
Console.WriteLine(sum);
2) 当您在循环的一个步骤中调用ReadLine两次时,逻辑是错误的。因此,您只能将偶数行添加到数组中。错误消息非常清楚-您正在传入null
一个不应该传入的位置。您是否尝试过使用调试器单步执行代码?每个循环迭代两次rea.ReadLine()
。P.S.File.ReadLines(@“c:\users\user\desktop\try.txt”).Sum(int.Parse)
我正在尝试读取一个文本文件,其中包含以下数字数组:١٣٥٦٩٦。这些是印地语数字,不是阿拉伯语;请发布错误消息并将跟踪堆栈为文本,而不是图像。Sum()
使用lambada,因此您不需要先对其执行。选择()。另外,请参见@CodingYoshi的答案。它清楚地总结了这个问题,提供了几个备选方案,并明确了失败的确切原因(读取最后一行将在下一行传递null)。非常感谢CodingYoshi,非常有用,它现在可以工作了