private void fileSelectButton_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog { InitialDirectory = Environment.CurrentDirectory, Filter = "text documents (*.txt)|*.txt" }; ofd.ShowDialog(); richTextBox1.Clear(); computePrimesButton.Enabled = false; if (string.IsNullOrWhiteSpace(ofd.FileName) || Path.GetExtension(ofd.FileName) != ".txt") //only take in .txt files { MessageBox.Show("Select a valid .txt file"); label1.Text = "No File Selected"; return; } _filePath = ofd.FileName; _numbersString = File.ReadLines(_filePath); _numbersString = _numbersString.Where(entry => !string.IsNullOrWhiteSpace(entry)); //remove empty entries caused by empty lines if (_numbersString.Any(entry => !FunctionRepository.CheckStringValue(entry))) { MessageBox.Show("All values in .txt file must be integers. Please reference your file for non-integer values"); return; } computePrimesButton.Enabled = true; label1.Text = Path.GetFileName(_filePath); }
private static void Main(string[] args) { IEnumerable <string> numbersString; if (!args.Any()) //if you run the exe by clicking then you wont have an input { //args = new string[] {Environment.CurrentDirectory + "\\test1.txt"}; Console.WriteLine( "Please drag txt file onto icon or use the command line to specify a target file to run program \nPress Enter To Continue"); Console.ReadLine(); return; } if (Path.GetExtension(args[0]) != ".txt") //only work with .txt files { Console.WriteLine("Please Select a File With the '.txt' Extension"); return; } try //try to read the sepecified file { numbersString = File.ReadLines(args[0]); } catch (FileNotFoundException) { Console.WriteLine("File Not Found. Please Enter a Valid File "); //Inform the user their file was not found at the specified locaton return; } numbersString = numbersString.Where(entry => !string.IsNullOrWhiteSpace(entry)); //remove empty entries caused by empty lines if (numbersString.Any(entry => !FunctionRepository.CheckStringValue(entry))) { Console.WriteLine( "All values in .txt file must be integers. Please reference your file for non-integer values \nPress Enter To Continue"); Console.ReadLine(); return; } List <int> numbers = numbersString.Select(int.Parse).ToList(); //create list of ints from list of strings from file foreach (int number in numbers) { string productString; Console.WriteLine(FunctionRepository.BuildOutputString(number, out productString)); } Console.WriteLine("\nPress Enter To Continue"); Console.ReadLine(); }
public void CheckStringValueTestValidInt() { Assert.IsTrue(FunctionRepository.CheckStringValue("1")); }
public void CheckStringValueTestNumberLargerThanInt32() { Assert.IsFalse(FunctionRepository.CheckStringValue("9000000000000000000000000")); }
public void CheckStringValueTestNonNumericString() { Assert.IsFalse(FunctionRepository.CheckStringValue("meh")); }
public void CheckStringValueTestWhiteSpace() { Assert.IsFalse(FunctionRepository.CheckStringValue(" ")); }
public void CheckStringValueTestInvalidInt() { Assert.IsFalse(FunctionRepository.CheckStringValue("1.3")); }
public void CheckStringValueTestNegativeInt() { Assert.IsTrue(FunctionRepository.CheckStringValue("-1")); }