0

I'm working on an assignment and I have to do alot of sorting and searching with arrays that have been read in from a file.

I'm relatively new to C# and keep getting this error.

enter image description here

Any help would be greatly appreciated as I don't have a long time until this work is due and I still have a fair ways to go.

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;
    }

}
}
PM.
  • 1,735
  • 1
  • 30
  • 38
  • 1
    What line does this happen on? What is the string being passed in? The cause is fairly obvious, but we can't run your program to find out what the values are, you need to tell us. – Ron Beyer Apr 17 '18 at 21:22
  • Possible duplicate of [Input string was not in a correct format](https://stackoverflow.com/questions/8321514/input-string-was-not-in-a-correct-format) – Richardissimo Apr 17 '18 at 22:33
  • The exception tells you what has happened and shows the line number. Your trying to convert a string to an integer but the string is not a valid integer. Use the debugger to follow the execution of your program, or at least put some print statements in appropriate places so you can see what your variables look like. – Matt Apr 17 '18 at 23:56

2 Answers2

1

The error is happening in the line with the following code:

int[] ReadFile = Array.ConvertAll(StringArray, int.Parse);

There is something wrong with an item inside your StringArray.

From your logic, you are trying to Parse some string that is not "parsable" to Integer, and this is generating the Error.

From Bottom to Top in the StackTrace, you can find which method in your code generated the error.

This might be useful in the future: Exception.StackTrace

Caio Melo
  • 51
  • 3
0

It means that some lines in some of your files cannot be parsed as an integer. Perhaps you have an empty line, blank, or some other character which isn't valid in a number.

Run your program in a debugger, line by line, and find in which file program breaks. Then look at the file to find a suspicious line.