Hello,
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:
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)
2, 32, 4, 7
4, 8, 3, 8
33, 11, 56, 65
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:
so for the 0 elements I would have an array (if using the above text example):
zeroArray[] = 2, 4, 33
thirdArray[] = 7, 8, 65
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)
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.
This is the problem I am having with this.
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.
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:
2
4
33
If i print intArray[3], it prints out all of the 4th integers within the text data like so:
7
8
65
I'm 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.
I have attempted to make arrays to store this data but no matter what I try, it doesn't work.
Instead of zeroArray[0] = 2, when I print it, it gives me the whole list:
2
4
33
If I then print zeroArray[1], expecting 4, it once again prints out the whole list
2
4
33
This is no longer in the code I will attach, I have tried several methods, but I am hoping someone knows of a solution
Unfortunately I can really only use loops and 1D arrays (no OOP or 2D arrays, which makes things easier)
Any help would be greatly appreciated, thank you.
` 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`