Hi, recently i have imported aload of data from a text file into my application, this data was then shown on screen within a rich textbox.
After which i wished to count the amount of same integers that occur on each line of the text file and then display the outcome onto the form in my GUI application.
This i managed to do but my code is so messy and they simply must be an easier way of doing things.
I have made a good attempt at this, so what i need to know is how can i make my 45 line code down to about 15 lines and also make it capable if a file is uploaded which has more than 4 lines of code?
I will explain the concept of my code below firslty. Basically i have a class, this class will load the data from the imported CSV file and then put it into a textbox. So within the class firslty a streamreader is created which will look through the file one line at a time using readline.
Next i create an array of the line and look through each element of the array. I then have a switch statement to look for the different numbers in the array depending on the case. e.g. case '0' will look for all 0's on the current line in the code.
I then do a check and add it to what row number it is on e.g. i have 4 row numbers, one of each line of the imported file. If the row is equal to 1 and the column is 1, it will add all values toether from that line with the value 1 and then display them.
It does this check for all numbers in the range 1-5 on each line/row. Once it reaches row 4 it finishes and displays the values within a textbox.
If this isn't clear my code can explain more.......and yes you will be sat there right now thinking "oh my god, what has he done here". which is why i need to know how i can get around the tangle i am in. What if a file with 20 lines was imported and each line had 100 numbers ranging from 1-100? yep that's alot of coding so i need a better solution.
MY COMMENTED CODE - Not very good effort
=============================
CODE
//first we create each row and let it be an array of 5 numbers
int [] row1 = new int [5];
int [] row2 = new int [5];
int [] row3 = new int [5];
int [] row4 = new int [5];
//next we assign a value to each number in the 4 arrays, i.e. 0
row1[0] = 0; row1[1] = 0; row1[2] = 0; row1[3] = 0; row1[4] = 0;
row2[0] = 0; row2[1] = 0; row2[2] = 0; row2[3] = 0; row2[4] = 0;
row3[0] = 0; row3[1] = 0; row3[2] = 0; row3[3] = 0; row3[4] = 0;
row4[0] = 0; row4[1] = 0; row4[2] = 0; row4[3] = 0; row4[4] = 0;
//we create a streamreader to read in the file from a path in a seperate object
StreamReader sr = new StreamReader(dataclass.path);
//create a string currentline which will equal the line the read is on, also create a cariable to trakc line number
string currentLine = sr.ReadLine();
int linenumber = 1;
//while the following is happening we do our case check on the chars in lines
while ((currentLine = sr.ReadLine()) != null)
{
linenumber++;
char[] chars = currentLine.ToCharArray();
foreach (char c in chars)
switch ©
{
case '0':
// 0 found
break;
case '1':
//if we find a number 1 we then do checks and begin to count the number of occurances of that integer, this is then added to the correct row array element
if (linenumber == 1) { row1[0] += 1; } if (linenumber == 2) { row2[0] += 1; } if (linenumber == 3) { row3[0] += 1; } if (linenumber == 4) { row4[0] += 1; }
break;
case '2':
if (linenumber == 1) { row1[1] += 1; } if (linenumber == 2) { row2[1] += 1; } if (linenumber == 3) { row3[1] += 1; } if (linenumber == 4) { row4[1] += 1; }
break;
case '3':
if (linenumber == 1) { row1[2] += 1; } if (linenumber == 2) { row2[2] += 1; } if (linenumber == 3) { row3[2] += 1; } if (linenumber == 4) { row4[2] += 1; }
break;
case '4':
if (linenumber == 1) { row1[3] += 1; } if (linenumber == 2) { row2[3] += 1; } if (linenumber == 3) { row3[3] += 1; } if (linenumber == 4) { row4[3] += 1; }
break;
case '5':
if (linenumber == 1) { row1[4] += 1; } if (linenumber == 2) { row2[4] += 1; } if (linenumber == 3) { row3[4] += 1; } if (linenumber == 4) { row4[4] += 1; }
break;
}
}
//once we have done checks on all lines for each integer in the range 1-5 we then write up the values within a rich textbox.
richTextBox1.Text += sr.ReadToEnd();
richTextBox1.Text += row1[0] + ":1 " + row1[1] + ":1 " + row1[2] + ":1 " + row1[3] + ":1 " + row1[4]+":1"+"\r";
richTextBox1.Text += row2[0] + ":2 " + row2[1] + ":2 " + row2[2] + ":2 " + row2[3] + ":2 " + row2[4]+":2"+"\r";
richTextBox1.Text += row3[0] + ":3 " + row3[1] + ":3 " + row3[2] + ":3 " + row3[3] + ":3 " + row3[4]+":3"+"\r";
richTextBox1.Text += row4[0] + ":4 " + row4[1] + ":4 " + row4[2] + ":4 " + row4[3] + ":4 " + row4[4]+":4";
}
//once this is done the values are displayed on screen.
Ok that is what i have done, as you can see the code is messy, what can i do to make the code alot less hastle? Again as i said earlier, the new code i would like the ability for it to handle more than 4 lines and more than 5 integers on each line. You think it is possible in much less code? if you can somebody please try and help as i know what i have done is the wrong way about doing it.
p.s. the file is comma seperated value and the fist element on each line is a name e.g. iamrow1,1,2,3,4,5,6
iamrownumber2,2,3,4,5,6,7
etc. you get the idea
thanks
This post has been edited by Sharkadder: 10 Apr, 2008 - 04:27 AM