C#拆分不起作用
我试图从文件中读取数据,拆分数据并保存到数组。除了拆分之外,代码工作正常。它正在返回一个C#拆分不起作用,c#,console-application,C#,Console Application,我试图从文件中读取数据,拆分数据并保存到数组。除了拆分之外,代码工作正常。它正在返回一个NullException 任何帮助都将不胜感激 public static void LoadHandData(CurrentHand[] handData, string fileName) { string input = ""; //temporary variable to hold one line of data string[] cardData; //
NullException
任何帮助都将不胜感激
public static void LoadHandData(CurrentHand[] handData, string fileName)
{
string input = ""; //temporary variable to hold one line of data
string[] cardData; //temporary array to hold data split from input
StreamReader readHand = new StreamReader(fileName);
for (int counter = 0; counter < handData.Length; counter++)
{
input = readHand.ReadLine(); //one record
cardData = input.Split(' '); //split record into fields
int index = 0;
handData[counter].cardSuit = Convert.ToChar(cardData[index++]);
handData[counter].cardValue = Convert.ToInt16(cardData[index++]);
}
readHand.Close();
}
公共静态void LoadHandData(CurrentHand[]handData,字符串文件名)
{
string input=”“;//用于保存一行数据的临时变量
string[]cardData;//用于保存从输入拆分的数据的临时数组
StreamReader readHand=新的StreamReader(文件名);
用于(int计数器=0;计数器
根据注释,您只有一行数据。但看看你的循环:
for (int counter = 0; counter < handData.Length; counter++)
{
input = readHand.ReadLine(); //one record
cardData = input.Split(' '); //split record into fields
int index = 0;
handData[counter].cardSuit = Convert.ToChar(cardData[index++]);
handData[counter].cardValue = Convert.ToInt16(cardData[index++]);
}
根据评论,您只有一行数据。但看看你的循环:
for (int counter = 0; counter < handData.Length; counter++)
{
input = readHand.ReadLine(); //one record
cardData = input.Split(' '); //split record into fields
int index = 0;
handData[counter].cardSuit = Convert.ToChar(cardData[index++]);
handData[counter].cardValue = Convert.ToInt16(cardData[index++]);
}
你从哪里得到例外?我怀疑你会发现你的文件中基本上没有足够的行。您的手数据有多大?文件有多长<代码> RealRead()/<代码>返回<代码> null <代码>,如果它到达了数据的末尾。您应该在读取行之后考虑一个保护克劳斯,以确保在拆分之前它不是空的。文件只是一行IE。C 12 S 12 C 7 H 5 D 2。我试图将char和int拆分为structsinput不为null的(手数据)数组。输入保存文件数据。如果您的文件只有一行,那么循环的第二次迭代不会在
readHand.ReadLine()
上返回null
?从哪里获得异常?我怀疑你会发现你的文件中基本上没有足够的行。您的手数据有多大?文件有多长<代码> RealRead()/<代码>返回<代码> null <代码>,如果它到达了数据的末尾。您应该在读取行之后考虑一个保护克劳斯,以确保在拆分之前它不是空的。文件只是一行IE。C 12 S 12 C 7 H 5 D 2。我试图将char和int拆分为structsinput不为null的(手数据)数组。输入保存文件数据。如果您的文件只有一行,则循环的第二次迭代不会在readHand.ReadLine()上返回null
?