views:

56

answers:

3

Hi all, I have my file names with the name

PPD_EntryDetailRecord_07192010.txt
PPD_EntryDetailRecord_07182010.txt
PPD_EntryDetailRecord_07162010.txt

In that the data will be like

6111111111111111111111111111125000000001111111111111111111111111111111111111111 1 6111111111111111111111111111150000000001111111111111111111111111111111111111111 1 611111111111111111111111111116500000.721111111111111111111111111111111111111111 1

Now I would like to add all those which were in bold and should display the sum...

A: 

If you are after the numbers in italic (not bold), and there are *s in the file, you can get the value like this (quick and dirty example code until you confirm the question):

string line = ""; // read a line from the file here
double lineValue = double.Parse(line.Split('*')[1]); // split by "*", get the second element, and parse as a double

You'd loop through all lines in the file and add lineValue to a sum variable.

Hope that helps.

Kieren Johnstone
Hi my values are 2500000000 ,5000000000,6500000.72 i would like to sum these
Dorababu
+2  A: 

The best I can make of it:

int start = "61111111111111111111111111111".Length + 1;
int length = "2500000000".Length;


string[] lines = System.IO.File.ReadAllLines(filename);
foreach(string line in lines)
{
  string data = line.SubString(start, length);
  double number = double.parse(data, CultureInfo.InvariantCulture);
  // sum it
}
Henk Holterman
actually i would like to sum the values in the 3 text files then how can i
Dorababu
@Dorababu: You repeat the last part 3 times.
Henk Holterman
A small help i am getting the text files to a list box and if i select multiple files from the list box and then click on generate i would like to add as above i asked then how can i achieve this
Dorababu
Use Listbox1.Selecteditems to get the filenames.
Henk Holterman
A: 

Case 1:

The numbers can have a variable number of digits, but cannot start or end with 1 digits.

You could use a regular expression pattern to extract the number from the file:

using System.Text.RegularExpressions;

double ExtractNumberFrom(string lineOfText)
{
    var regex = new Regex( @"61+(?<number>\d+?(\.\d+?)?)1+$" );

    if (regex.IsMatch(lineOfText))
    {
        var match = regex.Match(lineOfText);
        return double.Parse(match.Groups["number"].Value);
        // note: you might want some error handling for dealing with bad input!
    }
    else
    {
        return double.NaN;  // or any other form of error handling
    }
}

This solution recognizes numbers with 1 digits in the middle, but doesn't include 1s at the beginning or at the end.


Case 2:

The numbers have a fixed amount of digits and always start at the same position.

See Henk Holterman's answer. The idea is to simply extract a substring from the line of text which is at a specific position, using the string.Substring method. Then you continue parsing that substring into a double.

stakx