Reputation: 11
I have a problem with my C# project.
Suppose I have a textbox1 as follow (n rows x n columns):
0 1 0 1 0 1 0 1
0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0
Now I want to transfer data from this text box into an existed 2d Matrix that will store data in Integer Type.
I tried this but it seem to not works:
private void GETNUMERICDATA()
{
string txt = textbox1.text;
txt = txt.Replace(" ", string.Empty);
for (int k = 0; k < 32; k++)
{
for (int l = 0; l < 32; l++)
{
for (int i = 0; i < txt.Length; i++)
{
char chr = txt[i];
if (chr == '0')
{
Matrix[k, l] = (int)char.GetNumericValue('0');
}
else
{
if (chr == '1')
Matrix[k, l] = (int)char.GetNumericValue('1');
}
}
}
}
}
How do I do it?
Upvotes: 0
Views: 796
Reputation: 11
It's solved. Just modify in txt;
private void GETNUMERICDATA()
{
int currentPosition = 0;
string txt = textbox1.text;
txt = txt.Replace(" ", string.Empty);
txt = txt.Replace(Environment.Newline, string.Empty);
//Just add this code line
for (int k = 0; k < 32 && currentPosition < txt.Length; k++)
{
for (int l = 0; l < 32 && currentPosition < txt.Length; l++)
{
char chr = txt[currentPosition];
if (chr == '0')
{
Matrix[k, l] = 0;
}
else if (chr == '1')
{
Matrix[k, l] = 1;
}
currentPosition++;
}
}
}
Thanks all for your helping! Have a nice day.
Upvotes: 0
Reputation: 1067
The problem is the third loop over the input. You loop through the whole input every time. The result is, that after all loops have finished, the array will contain only the last value of your input. Try this:
private void GETNUMERICDATA()
{
int currentPosition = 0;
string txt = textbox1.text;
txt = txt.Replace(" ", string.Empty);
for (int k = 0; k < 32 && currentPosition < txt.Length; k++)
{
for (int l = 0; l < 32 && currentPosition < txt.Length; l++)
{
char chr = txt[currentPosition];
if (chr == '0')
{
Matrix[k, l] = (int)char.GetNumericValue('0');
}
else if (chr == '1')
{
Matrix[k, l] = (int)char.GetNumericValue('1');
}
currentPosition++;
}
}
}
Upvotes: 1