convert Text file 2D array to integer 2D array - c#

I am trying to use data from text file (matrix [i, j]). I have code works with one dimension array but doesn't work with two dimension array. I tried to use different method but I always get an error.
string fileContent = File.ReadAllText(file path);
string[] integerStrings = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
integers = new int[integerStrings.Length];
for (int n = 0; n < integerStrings.Length; n++)
integers[n] = int.Parse(integerStrings[n]);
I modify it to this
string fileContent = File.ReadAllText(path);
string[,] integerStrings = fileContent.Split(new char[,] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
integers = new int[integerStrings.Length,2];
for (int n = 0; n < integerStrings.Length; n++)
for (int j = 0; j <2; n++)
integers[n,j] = int.Parse(integerStrings[n,j]);
the text file
0 0
2 0
4 0
6 0
8 1
10 1
12 1
14 2
16 3
18 3
Note the code I need should be fixable with rows number

get the lines from the file then split each line to get your 2d array. Here is a rough first draft. You can test and refactor to improve on it if needed.
int[,] matrix = null;
int rowCount = 0;
int colCount = 0;
var lines = File.ReadAllLines(path);
rowCount = lines.Length;
for(int i = 0; i < rowCount; i++) {
var line = lines[i];
var tokens = line.Split(new []{' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
if(matrix == null) {
colCount = tokens.Length;
matrix = new int[rowCount, colCount];
}
for(int j = 0; j < colCount; j++) {
matrix[i, j] = int.Parse(tokens[j]);
}
}
this part is for display the matrix
int rowLength = matrix.GetLength(0);
int colLength = matrix.Rank;
for (int i = 0; i < rowLength; i++) {
for (int j = 0; j < colLength; j++) {
Console.Write(string.Format("{0} ", matrix[i, j]));
}
Console.WriteLine();
Console.WriteLine();
}
Console.ReadLine();

If you want 2d array split method just gives 1d array so you have to split twice...
First split by line breaks then by spaces...
string[] rows = fileContent.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int[,] result = new int[rows.Length,2];
for(int i = 0; i < rows.Length; i++)
{
var col = rows[i].Split(new char{' ','\t'}, StringSplitOptions.RemoveEmptyEntries);
result[i,0] = int.Parse(col[0]);
result[i,1] = int.Parse(col[1]);
}

Related

C#: Read a text file into a 2D char array

Like I mentioned above, I try to read a text file into a 2D char array.
That's my file:
abcde
fghij
klmno
pqrst
uvwxy
and that's my code:
var path = #"C:text.file";
StreamReader sr = File.OpenText(path);
{
char[,] arr = new char[5, 5];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
arr[i, j] = (Char)sr.Read();
Console.WriteLine(arr[i, j] + " = {0},{1}", i,j);
}
}
Console.WriteLine(arr[2,1]);
}
}
and at least my output:
1 = 0,0
2 = 0,1
3 = 0,2
4 = 0,3
5 = 0,4
= 1,0
= 1,1
6 = 1,2
7 = 1,3
8 = 1,4
9 = 2,0
0 = 2,1
= 2,2
...
so my question is, why e.g. arr[1,0] or arr[1,1] is empty?
thanks for your help!
sno0z3
The problem is caused by the presence of a newline at the end of each line (and a newline is composed of two characters in a Windows text file).
So you can check for these characters before adding to your array, or simply read the line (thus removing the newline) and then loop over the string obtained extracting char by char and adding them to your array
char[,] arr = new char[5, 5];
for (int i = 0; i < 5; i++)
{
string curLine = sr.ReadLine();
for (int j = 0; j < curLine.Length; j++)
{
arr[i, j] = curLine[j];
Console.WriteLine(arr[i, j] + " = {0},{1}", i,j);
}
}

split String[] with seperators and convert it to 2D Array

There is is a problem in my c# code.
I have a string array with 23 values in each line, seperated by a semicolon.
I want to split each value and parse it into an 2D [double] Array that should look like:
[(number of lines),22].
The string array looks like:
[0]
0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00
[1]
0,00;120,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00
The double array should look like:
[0,0] 0,00
[0,1] 111,00
[0,2] 0,00
[0,3] -1,00
and so on.
Do you have any ideas?
This is my current Code, that does not work.
double[,] values = new double[z, 22];
char[] seperator = { ';' };
int x = 0;
for (int i = 0; i < z; i++) {
for (int j = 0; j < 22; j++) {
values[i, j] = Data[x].Split(seperator);
x++;
}
}
How you could achieve this:
I use decimal here if you use double it will get the result 0 from a string like 0,00. So you can use double but if it can it will shorten it. Whit decimal a string like 0,00 will be 0.00
string[] arr = new string[] { "0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00" , "0,00;120,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00" };
// array conaining decimals
decimal[,] numbers = new decimal[2,23];
// loop thrue strings in arr
for (int i = 0; i < arr.Length; i++)
{
// split that string by ';'
string[] numberStrings = arr[i].Split(';');
// loop thrue the result of the splitted strings
for (int j = 0; j < numberStrings.Length; j++)
{
// parse that number from splitted string to decimal an put it on his place in the 2d array
numbers[i, j] = decimal.Parse(numberStrings[j]);
}
}
Please, try this code.
String a1 = "0,00;111,00;0,00;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,00;0,10;-0,10;-1,00;-1,00;0,00;0,00;0,00;0,00;0,00";
// new array split string by ';'
String[] arr1 = a1.Split(';');
int count = 0;
Console.WriteLine("\n\n\n-----------\nType 1 (only 1 array) \n-----------\n\n\n");
while ( count <= arr1.Length -1){
Console.WriteLine(arr1[count].ToString());
count++;
}
Console.WriteLine("\n\n\n-----------\nType 2 (multidimensional array) \n-----------\n\n\n");
// new array split string by ';' and ','
String[] arr2 = a1.Split(';');
string[,] arrFinal = new string[23,2];
// re-start counter
count = 0;
while (count <= arr2.Length - 1)
{
arrFinal[count, 0] = arr2[count].ToString().Split(',')[0];
arrFinal[count, 1] = arr2[count].ToString().Split(',')[1];
Console.WriteLine(arr2[count].ToString());
Console.WriteLine("item ({0},{1}) = {2} | item ({3},{4}) = {5} ", count, "0", arrFinal[count, 0], count, "1", arrFinal[count, 1], arrFinal[count, 1] );
count++;
}

Formatting into a Matrix form

I try using the code they have on msdn: it uses foreach and that puts each element on a different line.
How would I put it into a 3x3 matrix format?
char[] delimiterChars = { ' ', ',', '.', ':', '\t' ,'[' ,']', ';', '"', 'A', '=' };
string text = "A = [5 4 1; 3 6 1; 2 3 9]";
System.Console.WriteLine("Original text: '{0}'", text);
string[] words = text.Split(delimiterChars);
System.Console.WriteLine("{0} words in text:", words.Length);
foreach (string element in words)
{
System.Console.WriteLine(element);
}
// Keep the console window open in debug mode.
System.Console.WriteLine("Press any key to exit.");
System.Console.ReadKey();
Thanks in advance!
There are at least two ways to do that.
1.
At first your example will output
17 words in text:
This is because when you split string by all of the specified chars you get many empty strings, to get rid of them add StringSplitOptions.RemoveEmptyEntries option to Split method.
string[] words = text.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
Now you will get
9 words in text:
You can get 3 x 3 matrix with simple for loop, like
string[,] matrix = new string[3, 3];
for (int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
matrix[i, j] = words[i * 3 + j];
}
}
However you need to know number of rows and columns (or assume that the matrix is square).
The method will look something like this
public static string[,] GetMatrix1(string text, int n, int m)
{
char[] delimiterChars = { ' ', ',', '.', ':', '\t' ,'[' ,']', ';', '"', 'A', '=' };
string[] words = text.Split(delimiterChars, StringSplitOptions.RemoveEmptyEntries);
string[,] matrix = new string[n, m];
for (int i = 0; i < n; ++i)
{
for(int j = 0; j < m; ++j)
{
matrix[i, j] = words[i * m + j];
}
}
return matrix;
}
2.
Another way is to split text by [ and ] first, then by ; to get rows and at last by (space) to get elements in each row.
char[] delimiter1 = { '[', ']' };
char[] delimiter2 = { ';' };
char[] delimiter3 = { ' ' };
string[][] words = text.Split(delimiter1)[1]
.Split(delimiter2, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(delimiter3, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
There you will get string[][], number of columns may vary for each row, but if you want string[,] type you need to convert it.
As the result the implementation will be like this one
public static string[,] GetMatrix2(string text)
{
char[] delimiter1 = { '[', ']' };
char[] delimiter2 = { ';' };
char[] delimiter3 = { ' ' };
string[][] words = text.Split(delimiter1)[1]
.Split(delimiter2, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Split(delimiter3, StringSplitOptions.RemoveEmptyEntries))
.ToArray();
string[,] matrix = new string[words.Length, words[0].Length];
for(int i = 0; i < words.Length; ++i)
{
for(int j = 0; j < words[i].Length; ++j)
{
matrix[i, j] = words[i][j];
}
}
return matrix;
}

Read rows/columns of numbers in a text file (c#)

I have a text file with the following numbers for example:
1 2 3
4 5 6
How do I read each individual number into an array?
It needs to move along each row so read in 1, 2 and then 3 into a array of then move to the second row and read 4 5 and 6.. and show the number of cells in a textbox.
this is my code, but I got an error.
List<string> list2 = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
char[] chars = lines[i].ToCharArray();
for (int j = 0; j < chars.Length; j++)
{
if (!Char.IsWhiteSpace(chars[j]))
list2.Add(chars[j].ToString());
}
}
You could convert it to a jagged int array:
int[][] result = text
.Split('\n')
.Select(line =>
line
.Split(' ')
.Select(numberText => int.Parse(numberText))
.ToArray()).ToArray();
Iterate through like this:
foreach (int[] line in result)
{
Console.WriteLine(string.Join(", ", line)); // or iterate through its numbers again
// or whatever you want to do
}
Edit
List<string> list2 = new List<string>();
string path = "xy"; //your path
string[] lines = File.ReadAllLines(path);
int[][] result = lines
.Select(line =>
line
.Split(' ')
.Select(numberText => int.Parse(numberText))
.ToArray()).ToArray();
foreach (int[] line in result)
{
list2.Add(string.Join(", ", line)); // or whatever format you want the text to be
}
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label3.Text = openFileDialog1.FileName;
textBox1.Text = File.ReadAllText(label3.Text);
FileStream fs = File.OpenRead(label3.Text);
string[] lines = new StreamReader(fs).ReadToEnd().Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
fs.Close();
List<string> list2 = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
char[] chars = lines[i].ToCharArray();
for (int j = 0; j < chars.Length; j++)
{
if (!Char.IsWhiteSpace(chars[j]))
list2.Add(chars[j].ToString());
}
}
}

How to convert jagged array to 2D array?

I have a file file.txt with the following:
6,73,6,71
32,1,0,12
3,11,1,134
43,15,43,6
55,0,4,12
And this code to read it and feed it to a jagged array:
string[][] arr = new string[5][];
string[] filelines = File.ReadAllLines("file.txt");
for (int i = 0; i < filelines.Length; i++)
{
arr[i] = filelines[i].Split(',').ToArray();
}
How would I do the same thing, but with a 2D array?
Assuming you know the dimensions of your 2D array (or at least the maximum dimensions) before you start reading the file, you can do something like this:
string[,] arr = new string[5,4];
string[] filelines = File.ReadAllLines("file.txt");
for (int i = 0; i < filelines.Length; i++)
{
var parts = filelines[i].Split(','); // Note: no need for .ToArray()
for (int j = 0; j < parts.Length; j++)
{
arr[i, j] = parts[j];
}
}
If you don't know the dimensions, or if the number of integers on each line may vary, your current code will work, and you can use a little Linq to convert the array after you've read it all in:
string[] filelines = File.ReadAllLines("file.txt");
string[][] arr = new string[filelines.Length][];
for (int i = 0; i < filelines.Length; i++)
{
arr[i] = filelines[i].Split(','); // Note: no need for .ToArray()
}
// now convert
string[,] arr2 = new string[arr.Length, arr.Max(x => x.Length)];
for(var i = 0; i < arr.Length; i++)
{
for(var j = 0; j < arr[i].Length; j++)
{
arr2[i, j] = arr[i][j];
}
}

Categories