Search code examples
c#streamreaderwindows-console

How can I simulate user input from a console?


Im doing some challenges in HackerRank. I usually use a windows Form project in visualstudio to do the debug, but realize I lost lot of time input the test cases. So I want suggestion of a way I can easy simulate the console.ReadLine()

Usually the challenges have the cases describe with something like this:

5
1 2 1 3 2 
3 2

And then is read like: using three ReadLine

static void Main(String[] args) {
    int n = Convert.ToInt32(Console.ReadLine());
    string[] squares_temp = Console.ReadLine().Split(' ');
    int[] squares = Array.ConvertAll(squares_temp,Int32.Parse);
    string[] tokens_d = Console.ReadLine().Split(' ');
    int d = Convert.ToInt32(tokens_d[0]);
    int m = Convert.ToInt32(tokens_d[1]);
    // your code goes here
}

Right now I was thinking in create a file testCase.txt and use StreamReader.

using (StreamReader sr = new StreamReader("testCase.txt")) 
{
    string line;
    // Read and display lines from the file until the end of 
    // the file is reached.
    while ((line = sr.ReadLine()) != null) 
    {
        Console.WriteLine(line);
    }
}

This way I can replace Console.ReadLine() with sr.ReadLine(), but this need have a text editor open, delete old case, copy the new one and save the file each time.

So is there a way I can use a Textbox, so only need copy/paste in the textbox and use streamReader or something similar to read from the textbox?


Solution

  • You can use the StringReader class to read from a string rather than a file.