Note that there are some explanatory texts on larger screens.

plurals
  1. POprinting 2d array in c# through for loop
    text
    copied!<p>This is my filing function that takes data from the file and places it in the array:</p> <pre><code>public void populate_grid_by_file() { String store_data_from_file = string.Empty; try { using (StreamReader reader = new StreamReader("data.txt")) { store_data_from_file = reader.ReadToEnd().ToString(); } } catch (Exception e) { Console.WriteLine(e.Message); } string[] line = store_data_from_file.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i &lt; line.Length; i++) { string[] alphabet = store_data_from_file.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries); for (int j = 0; j &lt; alphabet.Length; j++) { Sodoku_Gri[i, j] = alphabet[j]; } } } </code></pre> <p>Here is whats written in the file:</p> <pre>1--2--3-- 3-4-4-5-- -7-3-4--- 7--5--3-6 --7---4-- 3-2--4-5- ------3-- 2-6--7--- 4---4--3-</pre> <p>This is what I want it to print:</p> <pre>1 - - 2 - - 3 - - 3 - 4 - 4 - 5 - - - 7 - 3 - 4 - - - 7 - - 5 - - 3 - 6 - - 7 - - - 4 - - 3 - 2 - - 4 - 5 - - - - - - - 3 - - 2 - 6 - - 7 - - - 4 - - - 4 - - 3 - </pre> <p>I thought of doing it this way:</p> <pre><code>public void display_grid() { for (int row = 0; row &lt; Sodoku_Gri.GetLength(0); row++) { for (int col = 0; col &lt; Sodoku_Gri.GetLength(1); col++) { Console.Write(Sodoku_Gri[row, col]); Console.Write(" "); } Console.WriteLine(); } } </code></pre> <p>I just can't understand why the 2d array printed 9 times with additional ------------ in the last row!</p> <p>It should be a 2d array with spacing between each element like I showed the data in the file but that is without spacing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload