我正在进行一项任务,我必须对从文件中读入的数组进行大量排序和搜索。
我对C#比较新,并且不断收到此错误。
任何帮助都会非常感激,因为我没有很长时间才能完成这项工作,而且我还有很长的路要走。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Bank_Data
{
class Program``
{
static void Main(string[] args)
{
//These Lines Read in all of the text files holding data about each of the share prices.
int[] Close_128 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Close_128.txt"));
int[] Close_256 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Close_256.txt"));
int[] Close_1024 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Close_1024.txt"));
int[] Change_128 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Change_128.txt"));
int[] Change_256 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Change_256.txt"));
int[] Change_1024 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Change_1024.txt"));
int[] High_128 = InsSort(System.IO.File.ReadAllLines(@"Data Files\High_128.txt"));
int[] High_256 = InsSort(System.IO.File.ReadAllLines(@"Data Files\High_256.txt"));
int[] High_1024 = InsSort(System.IO.File.ReadAllLines(@"Data Files\High_1024.txt"));
int[] Open_128 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Open_128.txt"));
int[] Open_256 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Open_256.txt"));
int[] Open_1024 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Open_1024.txt"));
int[] Low_128 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Low_128.txt"));
int[] Low_256 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Low_256.txt"));
int[] Low_1024 = InsSort(System.IO.File.ReadAllLines(@"Data Files\Low_1024.txt"));
int[] Merge_128 = MergeArrays(Close_128, High_128);
int[] Merge_256 = MergeArrays(Close_256, High_256);
int[] Merge_1024 = MergeArrays(Close_1024, High_1024);
string[] FileList = { "Close_128", "Close_256", "Close_1024", "Change_128", "Change_256", "Change_1024", "High_128", "High_256", "High_1024", "Open_128", "Open_256", "Open_1024", "Low_128", "Low_256", "Low_1024" };
foreach(int value in Merge_128)
{
Console.WriteLine(value);
}
Console.WriteLine("The files you may select are the following:");
foreach(string file in FileList)
{
Console.WriteLine(file);
string UserChoice = Console.ReadLine();
foreach(string choice in FileList)
{
if(UserChoice==choice)
{
BinarySort(choice);
}
}
}
}
static int[] MergeArrays(int[] Array1, int[] Array2)
{
int[] NewArray = new int[Array1.Length + Array2.Length];
Array.Copy(Array1, NewArray, Array1.Length);
Array.Copy(Array2, 0, NewArray, Array1.Length, Array2.Length);
List<int> Unsorted = new List<int>();
List<int> Sorted = new List<int>();
Sorted = MergeSort(Unsorted);
int[] sortedA = new int[Sorted.Count];
int counter = 0;
foreach (int number in Sorted)
{
sortedA[counter] = number;
counter++;
}
return sortedA;
}
static List<int> MergeSort(List<int> Unsorted)
{
List<int> left = new List<int>();
List<int> right = new List<int>();
List<int> sorted = new List<int>();
int middle = Unsorted.Count / 2;
for(int i =0;i<middle;i++)
{
left.Add(Unsorted[i]);
}
for(int i = middle;i<Unsorted.Count;i++)
{
right.Add(Unsorted[i]);
}
left = MergeSort(left);
right = MergeSort(right);
sorted = MergeSortLists(left, right);
return sorted;
}
static List<int> MergeSortLists(List<int> left,List<int> right)
{
List<int> result = new List<int>();
while(left.Count>0 || right.Count>0)
{
if(left.Count>0 && right.Count>0)
{
if(left.First() <= right.First())
{
result.Add(left.First());
left.Remove(left.First());
}
else
{
result.Add(right.First());
right.Remove(right.First());
}
}
else if(left.Count>0)
{
result.Add(left.First());
left.Remove(left.First());
}
else if(right.Count>0)
{
result.Add(right.First());
right.Remove(right.First());
}
}
return result;
}
static int[] InsSort(string[] StringArray)
{
int[] ReadFile = Array.ConvertAll(StringArray, int.Parse);
int AmountSorted = 1;
int counter;
while (AmountSorted < ReadFile.Length)
{
int temp = (ReadFile[AmountSorted]);
for (counter = AmountSorted; counter > 0; counter--)
{
if (temp < (ReadFile[counter - 1]))
{
ReadFile[counter] = ReadFile[counter - 1];
}
else
{
break;
}
}
(ReadFile[counter]) = temp;
AmountSorted++;
}
return ReadFile;
}
static string BinarySort(string select)
{
//this function hasnt been started yet.
return null;
}
}
}
答案 0 :(得分:1)
错误发生在包含以下代码的行中:
int[] ReadFile = Array.ConvertAll(StringArray, int.Parse);
StringArray中的项目有问题。
从你的逻辑来看,你试图解析一些不可解析的字符串&#34;到整数,这就产生了错误。
在StackTrace中从底部到顶部,您可以找到代码中的哪个方法生成错误。
这可能在将来有用:Exception.StackTrace
答案 1 :(得分:0)
这意味着某些文件中的某些行无法解析为整数。也许你有一个空行,空格或其他一些在数字中无效的字符。
在调试器中逐行运行程序,并查找哪个文件程序中断。然后查看文件以查找可疑行。