制表符分隔的txt文件从字符串数组读取int值 [英] Tab delimited txt file reading int value from string array

查看:138
本文介绍了制表符分隔的txt文件从字符串数组读取int值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TextReader来读取制表符分隔文件的每一行. 对于读取的每一行,我在选项卡上拆分并填充一个字符串数组. 然后,我可以访问字符串数组中的特定位置以获取值.

I am using TextReader to read in each line of a tab delimited file. For each line that it reads I split on the tabs and populate a string array. I can then access specific positions in the string array to get the values.

这非常适合以下字符串:userWorkload.SSN = arrColumns [0];
但是,当我尝试将列之一转换为int时,会在运行时收到错误消息:userWorkload.contactHours = Convert.ToInt32(arrColumns [9]);

This works great for strings like: userWorkload.SSN = arrColumns[0];
However, I get an error message at runtime when I try to convert one of the columns to an int: userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);

这是我的代码:

List<UserWorkload> userWorkloads = new List<UserWorkload>();
TextReader tr = File.OpenText("fall11-tab.txt");
string strLine = string.Empty;
string[] arrColumns = null;
while ((strLine = tr.ReadLine()) != null)
{
     UserWorkload userWorkload = new UserWorkload();

     arrColumns = strLine.Split('\t');
     userWorkload.SSN = arrColumns[0];
     userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);     

     userWorkloads.Add(userWorkload);
 }

和UserWorkload类仅包含简单的getter/setter:

and the UserWorkload class just contains simple getter / setters:

class UserWorkload 
{
     public string SSN { get; set; }
     public int contactHours { get; set; }
}

这是我的错误:
未处理的异常:System.FormatException:输入字符串的格式不正确.
在System.Number.StringToNumber(String str,NumberStyles选项,NumberBuffer& number,NumberFormatInfo信息,布尔值parseDecimal)处
at System.Number.ParseInt32(String s,NumberStyles样式,NumberFormatInfo信息)
在System.Convert.ToInt32(字符串值)上
在Snyder5Creator.InputFileReader.buildRowList()中的C:\ Users \ baxter.NET \ Documents \ Visual Studio 2010 \ Projects \ User5Creator \ User5Creator \ InputFileReader.cs:line 31
在C:\ Users \ baxter.NET \ Documents \ Visual Studio 2010 \ Projects \ User5Creator \ Snyder5Creator \ Program.cs:line 24中的Snyder5Creator.Program.Main(String [] args)中

Here is my error:
Unhandled Exception: System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Convert.ToInt32(String value)
at Snyder5Creator.InputFileReader.buildRowList() in C:\Users\baxter.NET\Documents\Visual Studio 2010\Projects\User5Creator\User5Creator\InputFileReader.cs:line 31
at Snyder5Creator.Program.Main(String[] args) in C:\Users\baxter.NET\Documents\Visual Studio 2010\Projects\User5Creator\Snyder5Creator\Program.cs:line 24

第31行是:userWorkload.contactHours = Convert.ToInt32(arrColumns [9]);

Line 31 is: userWorkload.contactHours = Convert.ToInt32(arrColumns[9]);

在此方面的任何帮助将不胜感激.

Any help on this would be greatly appreciated.

推荐答案

一个或多个记录中可能包含无效数据.如果您可以忽略这些错误,可以使用int.TryParse:

You probably have invalid data in one or more of the records. If you're OK with ignoring these errors, you could use int.TryParse:

 int parsedNumber;
 userWorkload.contactHours = int.TryParse(arrColumns[9], out parsedNumber) ? parsedNumber : -1;

这篇关于制表符分隔的txt文件从字符串数组读取int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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