Question

I have a 2-Dimensional array and a loop that prints all of the elements in the array using Console.Write and Console.WriteLine. However, the command line does not show all of the elements of the array. I tried debugging by also printing the output to a text file. When I look at the text file I see that it contains part of the expected output and the command line contains the other part of the expected output with a few overlaps. Does anyone know a possible solution to this problem?

public void Print()
{
    TextWriter tw = new StreamWriter("output.txt");
    //Prints the accessed coordinates and how many times it was accessed
    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 25; j++)
        {
            Console.Write("Coordinate " + grid[i, j].getCoordinates() + ": ");
            Console.WriteLine(grid[i, j].getAccessed()); 
            string text = "Coordinate " + grid[i,j].getCoordinates() + ": " + grid[i,j].getAccessed() + "";
            tw.WriteLine(text);
        }
    }
}
Was it helpful?

Solution

I think you are exceeding how many lines the console can display.

Try this: Console.BufferHeight = 500;

OTHER TIPS

I don't catch any errors in your code, However you can do two things.

Enable AutoFlush before doing any writes to the stream and check out the file.

1.

TextWriter tw = new StreamWriter("c:\\textwriter.txt");
tw.AutoFlush = true;

or

public void Print()
{
    TextWriter writeFile = new StreamWriter("c:\\textwriter.txt");

    for(int i = 0; i < 20; i++)
    {
        for(int j = 0; j < 25; j++)
        {
            writeFile.WriteLine("Coordinate " + grid[i,j].getCoordinates() + ": " + grid[i,j].getAccessed() + "");
            writeFile.Flush();
        }
    }
    writeFile.Close();
    writeFile = null;

}

2.Implement exception handling.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top