10 Replies - 963 Views - Last Post: 01 November 2012 - 08:29 PM Rate Topic: -----

#1 owllight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Help with reading in a file and arrays

Posted 31 October 2012 - 01:50 PM

Hello,

I am currently working on a project where I have to read in a text file that contains 4 different pieces of input that must be separated by commas:

this is what would be in the text file:
24, 3, 40, 5
96, 9, 74, 9
0, 10, 55, 67
etc.

-----

Although the text file contains 4 different fields, I only need to use two of them for different calculations (the first ones and the last ones)

ex:
24, 3, 40, 5
I just need positions 0 and 3 of every line

Note, I can only use loops and basic arrays (no 2D arrays):

Basically I'm stuck at getting the input into the two different arrays, one to hold all of the 0 position integers, and another array to hold all of the 3rd position integers

I had no trouble reading from my text file, or spliting it. my main issue is converting the strings into int so that i can use them in calculations. This is basically what I need:

Array1 <-- I want this to hold all the 0 position integer values, 1 integer value stored in each element

Array2 <-- I want this to hold all of the 3rd position integer values, 1 integer value stored in each element

static void Main(string[] args)
        {
            //declarations
            string input = "";
            string[] splitResult;

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

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


            while (input != null)
            {
                //split the line
                splitResult = input.Split(',');

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

                string[] strings = splitResult;

                //converting into int
                int[] intInput = new int[strings.Length];
                for (int index = 0; index < strings.Length; index++)
                {
                    int.TryParse(strings[index], out intInput[index]);
                }//end for


                int array1 = intInput[0];
                int array2 = intInput[3];


                //read next line
                input = myStream.ReadLine();

            }//end while



I wanted to test it just to see if the integer values were stored correctly:

if my text file says:
24, 3, 40, 5
96, 9, 74, 9
0, 10, 55, 67

I would want array1[0] to be 24, array1[1] to be 96 and array1[2] to be 0
I would want array2[0] to be 5, array2[1] to be 9, and array2[2] to be 67

When I tested it with a:
Console.WriteLine(array1[0]);

it gave me the error:
"cannot apply indexing with [] to an expression of type 'int'


I'm new to programming so I'm sure it is a simple mistake, but I would appreciate any help! I'm not sure exactly what I did wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Help with reading in a file and arrays

#2 ramsmack   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 11

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 02:16 PM

int[] intInput = new int[strings.Length];
for (int index = 0; index < strings.Length; index++)
{
   int.TryParse(strings[index], out intInput[index]);
}//end for


this is where your problem is, you are trying to pass an argument of type string into an array of type int, this will not work. try to parse the integers out of the string first, and then saving them into a different variable type.

This post has been edited by tlhIn`toq: 31 October 2012 - 02:41 PM
Reason for edit:: [code] your code here [/code] tags added

Was This Post Helpful? 0
  • +
  • -

#3 owllight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 02:42 PM

Thank you for the feedback

Yeah, I really am having difficulty converting the strings into integers. I understand now that first I need to covert the strings[] array into an int array, before storing it in an array declared as an int array.

This is what I could come up with for the actual conversion. I know it needs to go through a loop so each string could be converted:

int[] intInput;

for (int i = 0; i < strings.Length; i++) 
{
intInput[i] = int.tryparse(strings[i]);
}




Do I need a separate int variable to temporarily hold the int value before it can be placed in the int array?

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 02:45 PM

ramsmack, sorry but I don't see what you're seeing.

I don't see where the OP is trying to pass a string into an int[].

int.TryParse takes a string as the first parameter, tries to parse it, then places the int result of the parsing into the out (second) parameter which needs to be an int.

In this case the first parameter is a string and the second parameter is an int. The OP doesn't seem to be using this function improperly to me.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 02:50 PM

The problem with the OP's code is only that they have not initialized the array elements.

25 int[] intInput = new int[strings.Length];
This line makes a new array - but the elements of each array are not yet initialized.
Therefore, when you try to out a value into the array
int.TryParse(strings[index], out intInput[index]);
it doesn't really have a valid memory destination.

I might suggest that after you make the array, you loop through it setting all the elements to a known value. -1 is a common value in .NET for unset items. For example, if no item is selected in a combobox, the SelectedIndex will return -1. You'll have to decide what is a good known-preset value. Understand that .TryParse will not throw an exception. If someone tries to parse "YogiBear" it won't fail. It will just output a value of zero.

This post has been edited by tlhIn`toq: 31 October 2012 - 02:51 PM

Was This Post Helpful? 1
  • +
  • -

#6 owllight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 03:03 PM

Ok, so here is my while loop now:

 while (inputString != null)
            {
                //split the line
                fields = inputString.Split(',');

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

                string[] strings = fields;


                //setting all elements within intInput to the value of -1
                int[] intInput = new int[strings.Length];
                for (int i = 0; i < intInput.Length; i++)
                {
                    intInput[i] = -1;
                }

                Console.WriteLine(intInput[0]);


                //converting string to int
                for (int index = 0; index < strings.Length; index++)
                {
                    int.TryParse(strings[index], out intInput[index]);
                }//end for


                int agesInput = intInput[0];
                int districtsInput = intInput[3];

                Console.WriteLine(agesInput[1]);  //<--- error is here for 'agesInput[1]


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

            }//end while




I tested intInput[] right after setting them all to -1 (using Console.WriteLine) and it printed correctly multiple -1, so all elements within the intInput array are initialized to -1 before going through the TryParse.

I am still getting the same error unfortunately:
Cannot apply indexing with [] to an expression of type 'int'

Thank you for your help so far!
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 03:23 PM

It's just as the error says.
int agesInput = intInput[0];
Console.WriteLine(agesInput[1]);


Is agesInput an array?
Was This Post Helpful? 2
  • +
  • -

#8 owllight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 03:38 PM

Ah, thank you, I did forget to declare that my two arrays were actually arrays:

The error that it is giving me now:

"Cannot implicitly convert type 'int' to 'int[]'

From my understand it is saying that an int cannot be converted into an int array
all 3 of the arrays involved have been declared arrays:

string[] fields;
int[] intInput = new int[strings.Length];
string[] strings = fields;
int[] aInput;
int[] dInput;

I'm not sure as to what exactly the error is referring to

 while (inputString != null)
            {
                //split the line
                fields = inputString.Split(',');

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

                string[] strings = fields;
                int[] aInput;   // <-- added declaration
                int[] dInput;   // <-- added declaration


                //setting all elements within intInput to the value of -1
                int[] intInput = new int[strings.Length];
                for (int i = 0; i < intInput.Length; i++)
                {
                    intInput[i] = -1;
                }

                Console.WriteLine(intInput[0]);


                //converting string to int
                for (int index = 0; index < strings.Length; index++)
                {
                    int.TryParse(strings[index], out intInput[index]);
                }//end for


                aInput = intInput[0];  //<-- intInput[0] error
                dInput = intInput[3];  //<-- intInput[3] error

                Console.WriteLine(aInput[1]);


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

            }//end while


Was This Post Helpful? 0
  • +
  • -

#9 ramsmack   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-January 11

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 05:52 PM

aInput = intInput[0]; //<-- intInput[0] error
dInput = intInput[3]; //<-- intInput[3] error

the reason you are most likley getting this error is that dinput and ainput are array's, what your code says is save intInput[0](some value lets say 10) into aInput. so take 10 and put it into a ARRAY of aInput(
missing something) = intInput[0];
Was This Post Helpful? 0
  • +
  • -

#10 owllight   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 31-October 12

Re: Help with reading in a file and arrays

Posted 31 October 2012 - 06:39 PM

I've still been messing around with it, here is what I have now:

example text:
0, 22, 3, 4
8, 2, 6, 44
34, 5, 77, 89
96, 47, 0, 21

I don't have any errors, but this is my problem:

now if I did: Console.WriteLine(aInput[0]); it gives me the entire list of 0 elements, it prints out this:
0
8
34
96

This output stays the same even if I tried dInput[1] or dInput[2], it always prints all the numbers

If I do Console.WriteLine(dInput[0]); it gives me the entire 3rd position elements as well:

4
44
89
21

This output stays the same even if I tried dInput[1] or dInput[2], it always prints all the numbers

What I really need to do in order to do the calculations is have it so each number is stored in a certain element. For example:

aInput[0] should be 0 , aInput[1] should be 8, and so on and so forth
Likewise
dInput[0] should be 4, and dInput[1] should be 44, and so on and so forth

I forgot to mention, this program should be able to take a text file of any number, it could just have one row or twenty, that is why I can't hard code how many elements to make within an array

while (inputString != null)
            {
                //split the line
                fields = inputString.Split(',');

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

                string[] strings = fields;


                //setting all elements within intInput to the value of -1
                int[] intInput = new int[strings.Length];
                for (int i = 0; i < intInput.Length; i++)
                {
                    intInput[i] = -1;
                }


                //converting string to int
                for (int index = 0; index < strings.Length; index++)
                {
                    int.TryParse(strings[index], out intInput[index]);
                }//end for

                //creating new int arrays to hold int values
                int[] aInput = new int [intInput.Length];
                int[] dInput = new int[intInput.Length];
                for (int i = 0; i < intInput.Length; i++)
                {
                    aInput[i] = intInput[0];
                    dInput[i] = intInput[3];

                }//end for        

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

            }//end main while




Any additional help is appreciated! Thank you!
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 7445
  • View blog
  • Posts: 25,082
  • Joined: 05-May 12

Re: Help with reading in a file and arrays

Posted 01 November 2012 - 08:29 PM

The major issue you have is that on lines 26-27, you declare and allocate your aInput and dInput arrays. You need to declare the array outside your loop and increase the size of each array as you get a new row of data (and copy the data from the old array to the new array).

I don't know why you have the restriction that you are only allowed to use basic loops and arrays because this problem is crying out to be solved using a List<> so that you don't have to manage the array sizes and copy the data during each iteration.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1