This causes a exception in the line lists.Add(Convert.ToInt32(listInts)) īecause for the last line in the file generated listInts = " ". That means your file will end with and empty space instead a number. This line is writing an space always after a number. Sw.Write(" ") // create white space <- this line The problem in your code is inside the while loop, when you write to the file the white space: randomNumber = rnd.Next(1, 250) The other one is modifying the code you're using from the link you tell us. One of them is modifying the code you're using to create the file with the numbers. While (iterator ReadTestFile(string fileName) Static readonly string filePath = "randomNumbers.txt" This will ensure error-free string-to-int conversion.īelow, you can find a complete minimal working solution to your problem. Splitting the file contents string with StringSplitOptions.RemoveEmptyEntries option will exclude empty entries/substrings generated by the String.Split method. Split(separators, StringSplitOptions.RemoveEmptyEntries) Var separators = string.Join("", LineBreak, Delimiter).ToCharArray() Using (var streamReader = new StreamReader(fileStream)) Using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read)) While (iterator, you can use the following code: static List ReadTestFile(string fileName) Using (var streamWriter = new StreamWriter(fileStream)) Using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) Static void WriteTestFile(string fileName, To solve this issue, you can modify your method for writing the test data to a file as follows: static string Delimiter => " " However, the confusing part for me is when I manually type in the integers It works just fine.Īs it was mentioned by vetzascoa and Stas Ivanov, the exception in your code might be caused by the non-handled whitespaces and empty strings. ERROR: System.FormatException: 'Input string was not in a correctįrom what I can tell is when it is converting the integers, it grabs a value that isn't an integer and fails. The error that I am getting from my main class that stores these integers into an array list is failing at this point: lists.Add(Convert.ToInt32(listInts)) I created a separate class that creates a file of 100,000 random integers to properly test out the speed of the sorting algorithms. I have successfully recreated the mergesort, bubble, and insertion, and plan on implemented quicksort as well. My goal is to recreate sorting algorithms and have them race each other using forking. I am completely new to C#, but have experience in C/C++. I am using this link for a starting point.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |