Reading a text file

  • Thread starter Thread starter Nick
  • Start date Start date
N

Nick

Hiya,

Am having a few problems with StreamReader.

I'm trying to open a text file, and read 5 columns, and put them into an
array, and then do a calculation and read the next line of data, and do a
calc etc.

I can open the text file and display it fine, but cant seem to get each line
into an array to do the maths :(

Any help appreciated,
Rgds,
Nick
 
Nick, the columns should be separated by a means of a character, like a
tab. To get the strings and do the calculations you could try something
like the following


String[] tokens = line.Split("\t");
int[] numbers = new int[tokens.Length];

for (int i=0;i<numbers.Length;i++) {
try {
numbers = Convert.ToInteger(tokens);
} catch (Exception ex) {
numbers = -1; //this in case a number is malformed
}
}


and you will have your numbers in number array.

Regards,
Tasos
 
Thank you for your reply, the data in question looks like

2934477 LLWL 1.11 £0.00 (R) T[19.8 - 1.11 19.8]

I want the 1.11, then the 4 numbers in the [] brackets, the - would
sometimes have a number, sometimes not.

How would I be able to put these 4 numbers into an array exactly? The 4
numbers are odds, and the 3rd column is the winning hand.

Rgds,
Nick
 
Hiya,

Am having a few problems with StreamReader.

I'm trying to open a text file, and read 5 columns, and put them into an
array, and then do a calculation and read the next line of data, and do a
calc etc.

I can open the text file and display it fine, but cant seem to get each line
into an array to do the maths :(

Any help appreciated,
Rgds,
Nick

Nick,

Does the line have delimiters in it? Are the columns you want to use for the
calculations separated by commas or tabs or something like that?

If the line you are reading you can use the String.Split method to do exactly
what you are wanting to do.

Here is an example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string line = "10,21,10,17";

string[] cols = line.Split(new char[] { ',' });

int sum =
Convert.ToInt32(cols[0]) +
Convert.ToInt32(cols[1]) +
Convert.ToInt32(cols[2]) +
Convert.ToInt32(cols[3]);

Console.Write("the sum is {0}", sum);
Console.ReadLine();
}
}
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
Nick,

String[] tokens = line.Split("[");
String winHand = tokens[0].Split(" ")[2];
String[] numbers = tokens[1].Trim("]").Split[" "];

The winHand var has the winning hand.
The numbers array has the four numbers .

Regards,
Tasos

Thank you for your reply, the data in question looks like

2934477 LLWL 1.11 £0.00 (R) T[19.8 - 1.11 19.8]

I want the 1.11, then the 4 numbers in the [] brackets, the - would
sometimes have a number, sometimes not.

How would I be able to put these 4 numbers into an array exactly? The 4
numbers are odds, and the 3rd column is the winning hand.

Rgds,
Nick

Tasos Vogiatzoglou said:
Nick, the columns should be separated by a means of a character, like a
tab. To get the strings and do the calculations you could try something
like the following


String[] tokens = line.Split("\t");
int[] numbers = new int[tokens.Length];

for (int i=0;i<numbers.Length;i++) {
try {
numbers = Convert.ToInteger(tokens);
} catch (Exception ex) {
numbers = -1; //this in case a number is malformed
}
}


and you will have your numbers in number array.
 
Any chance you could do some code I can copy/paste and alter slightly to fit
in, as am finding this very fustrating, and usually learn by thoroughly
reading someone elses code and re-applying it lol. Will send you a few $$
for your troubles if you can,

Rgds,
Nick


Nick,

String[] tokens = line.Split("[");
String winHand = tokens[0].Split(" ")[2];
String[] numbers = tokens[1].Trim("]").Split[" "];

The winHand var has the winning hand.
The numbers array has the four numbers .

Regards,
Tasos

Thank you for your reply, the data in question looks like

2934477 LLWL 1.11 £0.00 (R) T[19.8 - 1.11 19.8]

I want the 1.11, then the 4 numbers in the [] brackets, the - would
sometimes have a number, sometimes not.

How would I be able to put these 4 numbers into an array exactly? The 4
numbers are odds, and the 3rd column is the winning hand.

Rgds,
Nick

Tasos Vogiatzoglou said:
Nick, the columns should be separated by a means of a character, like a
tab. To get the strings and do the calculations you could try something
like the following


String[] tokens = line.Split("\t");
int[] numbers = new int[tokens.Length];

for (int i=0;i<numbers.Length;i++) {
try {
numbers = Convert.ToInteger(tokens);
} catch (Exception ex) {
numbers = -1; //this in case a number is malformed
}
}


and you will have your numbers in number array.
 
This is not the point of learning...

you should try to develop the code yourself and face all the problems.

I've just provided the samples in order to show you the way to go, not
to solve your problem.

Regards,
Tasos
Any chance you could do some code I can copy/paste and alter slightly to fit
in, as am finding this very fustrating, and usually learn by thoroughly
reading someone elses code and re-applying it lol. Will send you a few $$
for your troubles if you can,

Rgds,
Nick


Nick,

String[] tokens = line.Split("[");
String winHand = tokens[0].Split(" ")[2];
String[] numbers = tokens[1].Trim("]").Split[" "];

The winHand var has the winning hand.
The numbers array has the four numbers .

Regards,
Tasos

Thank you for your reply, the data in question looks like

2934477 LLWL 1.11 £0.00 (R) T[19.8 - 1.11 19.8]

I want the 1.11, then the 4 numbers in the [] brackets, the - would
sometimes have a number, sometimes not.

How would I be able to put these 4 numbers into an array exactly? The 4
numbers are odds, and the 3rd column is the winning hand.

Rgds,
Nick

Tasos Vogiatzoglou said:
Nick, the columns should be separated by a means of a character, likea
tab. To get the strings and do the calculations you could try something
like the following


String[] tokens = line.Split("\t");
int[] numbers = new int[tokens.Length];

for (int i=0;i<numbers.Length;i++) {
try {
numbers = Convert.ToInteger(tokens);
} catch (Exception ex) {
numbers = -1; //this in case a number is malformed
}
}


and you will have your numbers in number array.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top