C
C#10mo ago
Array

Reading in a File 2D Arrays

I have to code a program that will read in a 2D array of 9 x 9 integers from a file that represents a Sudoku board solution My goals are to 1. Read in the file and display it on the form (it is a text file) 2. After this, I have to write a validator for the solution Right now, I’m focusing on part 1 which is reading in the file Usually, the goal when reading in a file is to do the following (Writing code to select the file) 1. OpenFileDialog oFD = new OpenFileDialog(); 2. if (oFD.ShowDialog() == DialogResult.OK) (File selected so open to read it) 3. StreamReader oSR = new StreamReader(oFD.OpenFile()); 4. int Records = int.Parse(oSR.ReadLine()); (Initializing the arrays) 5. Set array = new int [Records]; 6. for (int i = 0; i < array.length; i++) Array[i] = int.Parse(oSR.ReadLine()); This is the procedure I usually go through to read in a file My issue is since this is a 2D array, I get the error cannot implicitly (My code for this 2D array, I get an error when setting SudokuBoard to Records): https://paste.mod.gg/ttybylvutrvb/0 https://paste.mod.gg/gokonoflypqv/0( here is how I normally code it in) Anyone have ideas on how I code it in to read in the file?
BlazeBin - ttybylvutrvb
A tool for sharing your source code with the world!
BlazeBin - gokonoflypqv
A tool for sharing your source code with the world!
No description
No description
33 Replies
Buddy
Buddy10mo ago
Is it an exam?
Jimmacle
Jimmacle10mo ago
i swear i saw this same question asked earlier today
Array
ArrayOP10mo ago
No, just an assignment
MODiX
MODiX10mo ago
Array
I have to code a program that will read in a 2D array of 9 x 9 integers from a file that represents a Sudoku board solution My goals are to 1. Read in the file and display it on the form (it is a text file) 2. After this, I have to write a validator for the solution Right now, I’m focusing on part 1 which is reading in the file Usually, the goal when reading in a file is to do the following (Writing code to select the file) 1. OpenFileDialog oFD = new OpenFileDialog(); 2. if (oFD.ShowDialog() == DialogResult.OK) (File selected so open to read it) 3. StreamReader oSR = new StreamReader(oFD.OpenFile()); 4. int Records = int.Parse(oSR.ReadLine()); (Initializing the arrays) 5. Set array = new int [Records]; 6. for (int i = 0; i < array.length; i++) Array[i] = int.Parse(oSR.ReadLine()); This is the procedure I usually go through to read in a file My issue is since this is a 2D array, I get the error cannot implicitly (My code for this 2D array, I get an error when setting SudokuBoard to Records): https://paste.mod.gg/ttybylvutrvb/0 https://paste.mod.gg/gokonoflypqv/0( here is how I normally code it in) Anyone have ideas on how I code it in to read in the file?
Embed Type
Link
Quoted by
From Array
React with ❌ to remove this embed.
Jimmacle
Jimmacle10mo ago
please don't repost
Array
ArrayOP10mo ago
Ya I asked it but I didn’t get a response in a while, I thought I could post it again My bad Was worried it would get hidden since it’s been a couple of hours Sorry about this
Jimmacle
Jimmacle10mo ago
np, it just gets difficult to know what help you've already received if you're posting the exact same thing
Array
ArrayOP10mo ago
Does anyone have any ideas on how to do this with a 2d array? Angius was doing a good job but he went offline
Array
ArrayOP10mo ago
I think what confuses me here is that he never sets Records = to the 2d array, I usually did that before looping through
No description
SinFluxx
SinFluxx10mo ago
Well that pseudocode provided is for reading the lines from the file into the array, if that's what you mean
Array
ArrayOP10mo ago
Yea I know, I’m just a bit confused on it, like for example, how do I have line_idx > 9 if it is a 2d array? Also for The line = file_lines[line_idx], what does that mean?
SinFluxx
SinFluxx10mo ago
So the for loop would be reading through each of these lines from your input file:
No description
SinFluxx
SinFluxx10mo ago
so file_lines[0] = 75462913
Array
ArrayOP10mo ago
Yea I know this, I’m just actually confused on what line = file _lines[line_idx] really means. If I make an array with Sudoku.Length > 9, I will probably get an error again right Oh, do I have to code each individually?
SinFluxx
SinFluxx10mo ago
no, that's what the for loop would do for you
Array
ArrayOP10mo ago
Ya, I understand that (which is why I named my post reading in a file 2D arrays,) my next thing is actually coding this I think I am kinda confused on what line = file_lines means
SinFluxx
SinFluxx10mo ago
Your input file is made up of 9 lines file_lines would represent those 9 lines file_lines[line_idx] is a single line from those 9 lines
Array
ArrayOP10mo ago
Would SudokuBoard.GetLength represent the line index ? Also you realize the records variable i have, what does it actually do in my program Sorry if these are basic questions just trying to gain some understanding
SinFluxx
SinFluxx10mo ago
well StreamReader,ReadLine() in your input file would give you "759462913" as a string so then you're doing int.Parse("759462913"); which means Records = 759462913;
Array
ArrayOP10mo ago
I kinda get this I’m wondering though if file_lines means I actually like hard code everything
SinFluxx
SinFluxx10mo ago
I'm still not sure what you think you'd need to hardcode. You read all the lines to start with into file_lines, then you loop through all those lines one by one, then you'd loop through the line itself and insert each digit into your 2d array. The for loops will "automatically" go through each item (line) inside file_lines and each character in each line respectively What part are you thinking may need hardcoding?
Array
ArrayOP10mo ago
I was mainly looking at this example and seeing how you Parse’d the first line, thinking I’d have to do it for each
SinFluxx
SinFluxx10mo ago
I was just explaining what you were doing in your existing code 🙂
Array
ArrayOP10mo ago
Right, really the main concern is understanding what “file_lines” actually is as a variable and like “line”
SinFluxx
SinFluxx10mo ago
Well that's what I explained to you already:
You read all the lines to start with into file_lines
Or you can still use streamreader and just read it line by line you might be getting too caught up on the pseudocode, pseudocode is there to provide a "high level" explanation of what should be done in your actual code i.e. you need to: - get the data from the file - loop through each line of the data - loop through each character of each line - insert each character into your 2d array
Array
ArrayOP10mo ago
Hey, are you still there? Just got out of class now
SinFluxx
SinFluxx10mo ago
yeah (someone else can jump in even if the original person isn't around)
Array
ArrayOP10mo ago
My bad! Sorry
if (oFD.ShowDialog() == DialogResult.OK)
{
//file selected so open to read it
StreamReader oSR = new StreamReader(oFD.OpenFile());
int Records = int.Parse(oSR.ReadLine());
//initializing the arrays
for (int i = 0; i < SudokuBoard.GetLength(0); i++)
{

for (int j = 0; j < SudokuBoard.GetLength(1); j++)
{

}

}


}

if (oFD.ShowDialog() == DialogResult.OK)
{
//file selected so open to read it
StreamReader oSR = new StreamReader(oFD.OpenFile());
int Records = int.Parse(oSR.ReadLine());
//initializing the arrays
for (int i = 0; i < SudokuBoard.GetLength(0); i++)
{

for (int j = 0; j < SudokuBoard.GetLength(1); j++)
{

}

}


}

So I’ve added the 2 loops as you can see here My next step is to set Records = file_lines[line_idx], this is where I get confused though @SinFluxx Like I’m kinda confused but what is meant by file_lines, like I somewhat get we have to set it to it, although I’m not sure exactly what “file_lines” is Sorry if these are simple questions I’ve just been struggling a bit
SinFluxx
SinFluxx10mo ago
ok so file_lines is just a hypothetical name you might give to a variable where you're storing the contents of the file. And to use that way of doing things you'd read the whole file up front, rather than StreamReader where you'd read the file line-by-line i.e. you can use File.ReadAllLines() to read the whole file into an array, and use the for loops as above to loop through each line
Array
ArrayOP10mo ago
Oh okay, so it isn’t applicable here where I’m using StreamReader this right
SinFluxx
SinFluxx10mo ago
You wouldn't be accessing the lines from the file using an array if you're using StreamReader, no. Is StreamReader something you have to use?
Array
ArrayOP10mo ago
Yeah I can try asking my teacher about it
SinFluxx
SinFluxx10mo ago
it's fine to use so you'd instead need to do something like:
string line;
int lineIndex = 0;

using (var sr = new StreamReader(oFD.OpenFile()))
while ((line = sr.ReadLine()) != null)
{
// inner for loop goes here using
lineIndex++;
}
string line;
int lineIndex = 0;

using (var sr = new StreamReader(oFD.OpenFile()))
while ((line = sr.ReadLine()) != null)
{
// inner for loop goes here using
lineIndex++;
}
Want results from more Discord servers?
Add your server