Reading data from a file and separating it into multiple 1D arrays

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,<br/>
<br/>
I am working on a project and I am completely stuck. I basically need to read in text data from a file that is formatted like so:<br/>
<br/>
4 numbers per line separated by a comma (there is no hard set of how many rows, this program should be able to work on any number of rows)<br/>
<br/>
2, 32, 4, 7<br/>
4, 8, 3, 8<br/>
33, 11, 56, 65<br/>
<br/>
Basically all I need are the 0 elements and the 3rd(last) elements within this text document and I need to convert them into ints and place them into their own arrays:<br/>
<br/>
so for the 0 elements I would have an array (if using the above text example):<br/>
zeroArray[] = 2, 4, 33<br/>
thirdArray[] = 7, 8, 65<br/>
<br/>
So if i said zeroArray[1] it would be 4, if i said thirdArray[0] it would say 7 (*This is using the above example data, it needs to be able to work with any number)<br/>
<br/>
I need this data to be converted to ints and in their own arrays so that I can do calcuations to each element later on. I need to be able to call methods (that I will write) that will perform these calculations.<br/>
<br/>
This is the problem I am having with this. <br/>
<br/>
1. I cannot pull the intArray out of the while loop because it is declared within the while loop and that is its scope. But if I try and declare the intArray out of the while loop, it gives me the error use of unassigned local variable intArray. When I declare
it within the while loop, and attempt to use intArray outside of the while loop, it gives me the error "the name intArray does not exist in the current context. So my main problem is that I need to be able to access intArray so that I can use it as a parameter
for my methods to perform calculations on it.<br/>
<br/>
2. My second problem is intArray basically holds all 4 lines of data. If I print intArray[0], it prints out all of the 1st numbers listed, (*using the example text from above*) it prints:<br/>
2<br/>
4<br/>
33<br/>
If i print intArray[3], it prints out all of the 4th integers within the text data like so:<br/>
7<br/>
8<br/>
65<br/>
Im assuming what I need to do is make two more arrays, one to hold the 1st elements of text, and a second to hold the 4th elements of text, because these need to be processed differently.<br/>
<br/>
I have attempted to make arrays to store this data but no matter what I try, it doesnt work.<br/>
Instead of zeroArray[0] = 2, when I print it, it gives me the whole list:<br/>
2<br/>
4<br/>
33<br/>
If I then print zeroArray[1], expecting 4, it once again prints out the whole list<br/>
2<br/>
4<br/>
33<br/>
<br/>
Unfortunately I can only use loops and 1D arrays. I cannot use lists, OOP, or 2D arrays. So the coding isnt as efficient if I could use lists/2D arrays. <br/>
<br/>
I currently store the values into the int array intArray because I wasnt sure how to create a for loop that would be able to store the different values into two different arrays. I cant really think of a way to do so. Any method I have attempted has failed.<br/>
<br/>
Can I use two arrays instead of a List? intArray does hold the converted integer values (4 different sections and I only need 2 of them for calculcations). Is there anyway to create two arrays that hold intArray[0] and intArray[3] values so that I can proceed
to do calculations with them? Also how can I make it so I can use these two arrays outside the loop? If i create an array outside the loop, it wont let me use it within the loop, and if I create an array inside the loop, it wont let me use it outside of
the loop.<br/>
<br/>
Any help would be greatly appreciated


<pre class="prettyprint static void Main(string[] args)
{

//declarations
string inputString = "";
string[] results;
int holder = 0;

//import file for reading
FileStream inFile = new FileStream("input.txt", FileMode.Open, FileAccess.Read);
StreamReader myStream = new StreamReader(inFile);

//reads first line
inputString = myStream.ReadLine();


while (inputString != null)
{
//split the line
results = inputString.Split(,);
int[] intArray = new int[results.Length];


//do whatever processing you need to do to store it


for (int index = 0; index < results.Length; index++)
{
if (int.TryParse(results[index], out holder))
{
intArray[index] = holder;
}//end if
}//end for

//reads next line
inputString = myStream.ReadLine();



}//end while

//This is a test to see if the ints are correctly stored
Console.WriteLine(intArray[0]); //<--error here stating that The name intArray does not exist in the current context

}//end main[/code]
<br/>


View the full article
 
Back
Top