Answered by:
How to retrieve values from a string split and put it into a 2 dimensional array

Question
-
public static void ImportOutputPuzzle() { string[,] puzzle1 = new string[9, 9]; string line; List<string> puzzle = new List<string>(); using (var file = new StreamReader(@"C:\Users\John\Desktop\puzzle1.txt")) { while ((line = file.ReadLine()) != null) { var tokens = line.Split(','); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { foreach(var numbers in tokens) { puzzle1[i, j] = numbers; } } } }
I am reading in a file and splitting the values in the file by a comma and trying to get those values and put those values into a 2d array. The problem I am having is the way I'm doing it, all I get is 0's in my 2d array instead of the actual array. I know my split function is working properly because when I go to print numbers from my foreach loop I get the correct values. My question is how do I properly get the values from my string array tokens into a 2d array without it filling with just 0's?Saturday, January 28, 2017 9:55 PM
Answers
-
Here's a quick example of reading all lines from the file first,
before trying to fill the 2d array. It assumes that the file has
exactly 9 lines of nine numbers each.
string[,] puzzle1 = new string[9, 9]; string line; string[] tokens = new string[81]; int incr = 0; using (var file = new StreamReader(@"puzzle1.txt")) { while ((line = file.ReadLine()) != null) { var tokes = line.Split(','); Array.Copy(tokes, 0, tokens, incr, 9); incr += 9; } } int idx = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { puzzle1[i, j] = tokens[idx++]; } }
Error checking, etc. is left to your discretion.
- Wayne
- Marked as answer by Ishiii101 Sunday, January 29, 2017 8:27 AM
Sunday, January 29, 2017 7:54 AM
All replies
-
Hi,
why do you use 3 loops? You loop over each "row" and in it over each "column" and then set for each index in a new loop the values multiple times
foreach(var numbers in tokens) { puzzle1[i, j] = numbers;
will set the value for index i,j multiple times.
Regards,
Thorsten
- Edited by Thorsten Gudera Saturday, January 28, 2017 11:13 PM
Saturday, January 28, 2017 11:12 PM -
var tokens = line.Split(','); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { foreach(var numbers in tokens) { puzzle1[i, j] = numbers; } } } }
how do I properly get the values from my string array tokens into a 2d array without it filling with just 0's?You've got too many loops. Try something closer to this:
while ((line = file.ReadLine()) != null) { var tokens = line.Split(','); int idx = 0; // **** for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { //foreach (var numbers in tokens) //{ //puzzle1[i, j] = numbers; puzzle1[i, j] = tokens[idx++]; // **** //} } } }
The exact code depends on how many numbers in a line.
- Wayne
Saturday, January 28, 2017 11:21 PM -
The exact code depends on how many numbers in a line.
You should stipulate how the numbers are organized in the file.
As written your code will fill all slots in the 2d array from the data
in the first line of the file. If it doesn't have 81 numbers in it then
the suggested change I gave will overrun the tokens array. If it does have
81 numbers then you will fill the puzzle1 array from the first line. If
there is more than one line in the file, you will start refilling the
puzzle1 array all over again.
- Wayne
Saturday, January 28, 2017 11:32 PM -
Hey Wayne, the file is a a grid of numbers that are 9x9, it is basically a sudoku puzzle in the file, thanks for your help and explanation as to what I did wrong in my code, it makes more sense now as to why I was only getting one values filled up in my array and why your way of doing it makes more sense to me. I have tried your suggestion and it comes up with an index out of range error on the puzzle1[i,j]=tokens[indx++] line, i put the code through debug and looked at the values for each variable in my code, and the indx variable value ends being 10 at the end which throws an index out of range error. I thought my for loops would give it an upperbound of 8 for the variable but it seems it does not, i have also tried putting in an upperbound with an if statement making the value less than 9 but that only puts one line into my array. How do I put a proper upper bound for the indx variable so that the index does not go out of range?
Sunday, January 29, 2017 6:48 AM -
How do I put a proper upper bound for the indx variable so that the index does not go out of range?
I cautioned you that you would get an overrun on the tokens array if the
first (or only) line in the file had less than 81 numbers in it. I also
explained that you attempt to fill the entire array (9x9 = 81) after
reading only the first line from the file. That means that the tokens
array must have 81 strings in it or you try to use an index which is
too great.
I also stated quite clearly that the *exact* logic depends on how the data
in the file is organized. You still haven't described that clearly.
If the file has multiple,lines, such as 9 lines of 9 numbers each, then
you need to read all lines from the file and store all of the strings
into the tokens array *before* you start to fill the 2d array.
Alternatively, you need to have the file reading line by line *within*
the outer loop so that you can fill the array in the inner loop one
column (?) at a time.
The file also must be organized logically to get the 2d array filled
in the desired order.
- Wayne
Sunday, January 29, 2017 7:16 AM -
Here's a quick example of reading all lines from the file first,
before trying to fill the 2d array. It assumes that the file has
exactly 9 lines of nine numbers each.
string[,] puzzle1 = new string[9, 9]; string line; string[] tokens = new string[81]; int incr = 0; using (var file = new StreamReader(@"puzzle1.txt")) { while ((line = file.ReadLine()) != null) { var tokes = line.Split(','); Array.Copy(tokes, 0, tokens, incr, 9); incr += 9; } } int idx = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { puzzle1[i, j] = tokens[idx++]; } }
Error checking, etc. is left to your discretion.
- Wayne
- Marked as answer by Ishiii101 Sunday, January 29, 2017 8:27 AM
Sunday, January 29, 2017 7:54 AM -
Thank you for the response, I should have been specific about the file structure. Essentially the file looks like this:
0,0,0,0,0,0,0,0,0
0,8,9,4,1,0,0,0,0
0,0,6,7,0,0,1,9,3
2,0,0,0,0,0,7,0,0
3,4,0,6,0,0,0,1,0
0,0,0,9,0,0,0,0,5
0,0,0,0,2,0,0,5,0
6,5,0,0,4,0,0,2,0
7,3,0,1,0,0,0,0,0The file is structured with 9 lines with 9 numbers each, sorry I assumed that it would be understood when I mentioned that it was a sudoku puzzle, I should have been more specific. When you mentioned that the token variable would get overrun I interpreted your wording as 81 values total in the file not 81 lines on the first line. I will try the code and try to tweak it to get it to work properly. Also what exactly do you mean by the file being logically organized? Again, thanks for the help, sorry I misunderstood some of your wording.
Sunday, January 29, 2017 8:09 AM -
Also what exactly do you mean by the file being logically organized?
I meant the order of the numbers in each line and their intended
relationship to positions in the grid. Does each line represent all of
the numbers for a given column, or for a given row? etc.
- Wayne
Sunday, January 29, 2017 8:19 AM -
oh ok, that makes more sense, I got the code to work correctly with how I want the values organized in my 2d array. Thank you for your help, I appreciate it. Hope you have a nice day.Sunday, January 29, 2017 8:27 AM