469,929 Members | 1,381 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

add numbers in file

I have a text file that has a column of numbers that i need to add to get a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible from a
text file?
Nov 17 '05 #1
8 1655
I'm not quite sure I follow the question here but I'll take a stab. If I
don't understand then I'm sorry.

First of all there is no way to just ask the computer what the total is for
column 2. However, you can write a function that will open the text file,
read the file in one line at the time, grabbing the second column and
converting it to a double or currency and then getting the next line.

The text file is going to read in a string for each line that you will have
to parse to get to the 2nd column which looks quite easy if this is the
format of your file. But you still have a string when you parse it so you
will need to cast it as a double or currency in order to add it up as a
number value.

So from my point of view the problem is quite easily obtainable.

Good luck,

glenn

"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I have a text file that has a column of numbers that i need to add to get a total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible from a text file?

Nov 17 '05 #2
since i never done this before, do you have a snippet or can you point me to
an example of this being done?
"glenn" <gh******@softeksoftware.com> wrote in message
news:un*************@TK2MSFTNGP15.phx.gbl...
I'm not quite sure I follow the question here but I'll take a stab. If I
don't understand then I'm sorry.

First of all there is no way to just ask the computer what the total is for column 2. However, you can write a function that will open the text file,
read the file in one line at the time, grabbing the second column and
converting it to a double or currency and then getting the next line.

The text file is going to read in a string for each line that you will have to parse to get to the 2nd column which looks quite easy if this is the
format of your file. But you still have a string when you parse it so you
will need to cast it as a double or currency in order to add it up as a
number value.

So from my point of view the problem is quite easily obtainable.

Good luck,

glenn

"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I have a text file that has a column of numbers that i need to add to
get a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible
from a
text file?


Nov 17 '05 #3
Here are the very basics to get you started
this will open a file with specified path and name of settings.txt
StreamReader sr1 = File.OpenText( path + \\settings.txt);
String input;
//read through file and retreive info
string st = "";
while ((input=sr1.ReadLine())!=null)
{
st = input;
from here you just need to parse the string value held in st to look for
your field. I don't know what to tell you here, if its a space delimited
field then its really easy as all you have to do is give it a start and end
location through the use of the st.substring function to grab what you need.
Then to do the cast to the double for the purposes of adding up your values
you can use the Convert function...
}

good luck,

"microsoft.news.com" <CSharpCoder> wrote in message
news:eC*************@TK2MSFTNGP15.phx.gbl...
since i never done this before, do you have a snippet or can you point me to an example of this being done?
"glenn" <gh******@softeksoftware.com> wrote in message
news:un*************@TK2MSFTNGP15.phx.gbl...
I'm not quite sure I follow the question here but I'll take a stab. If I don't understand then I'm sorry.

First of all there is no way to just ask the computer what the total is

for
column 2. However, you can write a function that will open the text file, read the file in one line at the time, grabbing the second column and
converting it to a double or currency and then getting the next line.

The text file is going to read in a string for each line that you will

have
to parse to get to the 2nd column which looks quite easy if this is the
format of your file. But you still have a string when you parse it so you will need to cast it as a double or currency in order to add it up as a
number value.

So from my point of view the problem is quite easily obtainable.

Good luck,

glenn

"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I have a text file that has a column of numbers that i need to add to

get
a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible

from
a
text file?



Nov 17 '05 #4
Im reading the file fine, its the adding up the column I need help with.
I'm reading the file and inserting the data into a table. but now i need to
Total the sale price column and insert that into a new column
"glenn" <gh******@softeksoftware.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
Here are the very basics to get you started
this will open a file with specified path and name of settings.txt
StreamReader sr1 = File.OpenText( path + \\settings.txt);
String input;
//read through file and retreive info
string st = "";
while ((input=sr1.ReadLine())!=null)
{
st = input;
from here you just need to parse the string value held in st to look for
your field. I don't know what to tell you here, if its a space delimited
field then its really easy as all you have to do is give it a start and end location through the use of the st.substring function to grab what you need. Then to do the cast to the double for the purposes of adding up your values you can use the Convert function...
}

good luck,

"microsoft.news.com" <CSharpCoder> wrote in message
news:eC*************@TK2MSFTNGP15.phx.gbl...
since i never done this before, do you have a snippet or can you point me
to
an example of this being done?
"glenn" <gh******@softeksoftware.com> wrote in message
news:un*************@TK2MSFTNGP15.phx.gbl...
I'm not quite sure I follow the question here but I'll take a stab. If
I don't understand then I'm sorry.

First of all there is no way to just ask the computer what the total
is
for
column 2. However, you can write a function that will open the text file, read the file in one line at the time, grabbing the second column and
converting it to a double or currency and then getting the next line.

The text file is going to read in a string for each line that you will

have
to parse to get to the 2nd column which looks quite easy if this is
the format of your file. But you still have a string when you parse it so

you will need to cast it as a double or currency in order to add it up as a number value.

So from my point of view the problem is quite easily obtainable.

Good luck,

glenn

"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
> I have a text file that has a column of numbers that i need to add

to get
a
> total of. Is there any way to add the number column in my text file?
>
> the text file looks like this:
>
> BMW 25,252.00 1/1/2005
> BMW 19,000.00 3/15/2005
>
>
> I need to add the money colum to get 44,252.00. is that even
possible from
a
> text file?
>
>



Nov 17 '05 #5
I don't understand really what you are having trouble with. If you have a
variable holding your cost field and I assume it would be a string variable
you just call the Convert function like I said to convert it to a number.

string myvar;
myvar = fieldfromfile;
double item;
double total = 0;

item = Convert.ToDouble( myvar );
total += item;

at the end you will have total that will contain the total amount of items
in teh file...

Your questions and examples are not really shedding much light on exactly
what you are doing so if this doesn't answer you question then maybe someone
else can help you better...

good luck,

glenn
"microsoft.news.com" <CSharpCoder> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Im reading the file fine, its the adding up the column I need help with.
I'm reading the file and inserting the data into a table. but now i need to Total the sale price column and insert that into a new column
"glenn" <gh******@softeksoftware.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
Here are the very basics to get you started
this will open a file with specified path and name of settings.txt
StreamReader sr1 = File.OpenText( path + \\settings.txt);
String input;
//read through file and retreive info
string st = "";
while ((input=sr1.ReadLine())!=null)
{
st = input;
from here you just need to parse the string value held in st to look for
your field. I don't know what to tell you here, if its a space delimited
field then its really easy as all you have to do is give it a start and end
location through the use of the st.substring function to grab what you

need.
Then to do the cast to the double for the purposes of adding up your

values
you can use the Convert function...
}

good luck,

"microsoft.news.com" <CSharpCoder> wrote in message
news:eC*************@TK2MSFTNGP15.phx.gbl...
since i never done this before, do you have a snippet or can you point me
to
an example of this being done?
"glenn" <gh******@softeksoftware.com> wrote in message
news:un*************@TK2MSFTNGP15.phx.gbl...
> I'm not quite sure I follow the question here but I'll take a stab.

If
I
> don't understand then I'm sorry.
>
> First of all there is no way to just ask the computer what the total

is for
> column 2. However, you can write a function that will open the text

file,
> read the file in one line at the time, grabbing the second column and > converting it to a double or currency and then getting the next line. >
> The text file is going to read in a string for each line that you will have
> to parse to get to the 2nd column which looks quite easy if this is the > format of your file. But you still have a string when you parse it so you
> will need to cast it as a double or currency in order to add it up
as a > number value.
>
> So from my point of view the problem is quite easily obtainable.
>
> Good luck,
>
> glenn
>
> "microsoft.news.com" <CSharpCoder> wrote in message
> news:uX**************@TK2MSFTNGP09.phx.gbl...
> > I have a text file that has a column of numbers that i need to add to get
> a
> > total of. Is there any way to add the number column in my text
file? > >
> > the text file looks like this:
> >
> > BMW 25,252.00 1/1/2005
> > BMW 19,000.00 3/15/2005
> >
> >
> > I need to add the money colum to get 44,252.00. is that even

possible from
> a
> > text file?
> >
> >
>
>



Nov 17 '05 #6
Hi,

Of course it's
How to do it depend of how the file is create, by the description you gave
you have two options
1- The columns start in a fixed location
2- Thr columns are separated by a TAB

a seudo code;
string line;
double total = 0;
while( (line= file.ReadLine()) != null )
{
string[] parts = line.Split( "\t" ); // or line.SubString (
startIndex, endIndex );
try{
double += Convert.ToDouble( parts[1] );
}catch{}
}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I have a text file that has a column of numbers that i need to add to get a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible from
a
text file?

Nov 17 '05 #7
Hi Glenn,

The OP is a student trying to get you to help him with homework.

We have an "unwritten convention" of providing assistance without actually
providing code.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"glenn" <gh******@softeksoftware.com> wrote in message
news:em*************@TK2MSFTNGP12.phx.gbl...
I don't understand really what you are having trouble with. If you have a
variable holding your cost field and I assume it would be a string
variable
you just call the Convert function like I said to convert it to a number.

string myvar;
myvar = fieldfromfile;
double item;
double total = 0;

item = Convert.ToDouble( myvar );
total += item;

at the end you will have total that will contain the total amount of items
in teh file...

Your questions and examples are not really shedding much light on exactly
what you are doing so if this doesn't answer you question then maybe
someone
else can help you better...

good luck,

glenn
"microsoft.news.com" <CSharpCoder> wrote in message
news:OB**************@tk2msftngp13.phx.gbl...
Im reading the file fine, its the adding up the column I need help with.
I'm reading the file and inserting the data into a table. but now i need

to
Total the sale price column and insert that into a new column
"glenn" <gh******@softeksoftware.com> wrote in message
news:el**************@TK2MSFTNGP10.phx.gbl...
> Here are the very basics to get you started
> this will open a file with specified path and name of settings.txt
> StreamReader sr1 = File.OpenText( path + \\settings.txt);
> String input;
> //read through file and retreive info
> string st = "";
> while ((input=sr1.ReadLine())!=null)
> {
> st = input;
> from here you just need to parse the string value held in st to look
> for
> your field. I don't know what to tell you here, if its a space delimited > field then its really easy as all you have to do is give it a start and

end
> location through the use of the st.substring function to grab what you

need.
> Then to do the cast to the double for the purposes of adding up your

values
> you can use the Convert function...
> }
>
> good luck,
>
> "microsoft.news.com" <CSharpCoder> wrote in message
> news:eC*************@TK2MSFTNGP15.phx.gbl...
> > since i never done this before, do you have a snippet or can you
> > point

me
> to
> > an example of this being done?
> >
> >
> > "glenn" <gh******@softeksoftware.com> wrote in message
> > news:un*************@TK2MSFTNGP15.phx.gbl...
> > > I'm not quite sure I follow the question here but I'll take a stab.

If
> I
> > > don't understand then I'm sorry.
> > >
> > > First of all there is no way to just ask the computer what the
> > > total

is
> > for
> > > column 2. However, you can write a function that will open the
> > > text
> file,
> > > read the file in one line at the time, grabbing the second column and > > > converting it to a double or currency and then getting the next line. > > >
> > > The text file is going to read in a string for each line that you will > > have
> > > to parse to get to the 2nd column which looks quite easy if this is

the
> > > format of your file. But you still have a string when you parse it so > you
> > > will need to cast it as a double or currency in order to add it up as
a
> > > number value.
> > >
> > > So from my point of view the problem is quite easily obtainable.
> > >
> > > Good luck,
> > >
> > > glenn
> > >
> > > "microsoft.news.com" <CSharpCoder> wrote in message
> > > news:uX**************@TK2MSFTNGP09.phx.gbl...
> > > > I have a text file that has a column of numbers that i need to
> > > > add

to
> > get
> > > a
> > > > total of. Is there any way to add the number column in my text

file? > > > >
> > > > the text file looks like this:
> > > >
> > > > BMW 25,252.00 1/1/2005
> > > > BMW 19,000.00 3/15/2005
> > > >
> > > >
> > > > I need to add the money colum to get 44,252.00. is that even

possible
> > from
> > > a
> > > > text file?
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 17 '05 #8
Looks like homework. If I'm off base, let me know.

A file is like a page of paper. You don't read all the words at once... you
read them in order.
Your program has to read the words in order.

The other posters have provided basic code to open the file and read one
line at a time.
In each line, you need to find the values. Ignacio mentioned that you can
use the Split method on the String object to break up a single string into
an array of strings where each of the smaller ones contains a "word",
depending on how you define it.

You simply need to get a number from that small string (the decimal.Parse()
method comes to mind) and then add the values to a running total.

When you are done, you have your running total.

You can display this total or write it to another file or store it in a
database, depending on the needs of the application.

I hope this helps,

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"microsoft.news.com" <CSharpCoder> wrote in message
news:uX**************@TK2MSFTNGP09.phx.gbl...
I have a text file that has a column of numbers that i need to add to get a
total of. Is there any way to add the number column in my text file?

the text file looks like this:

BMW 25,252.00 1/1/2005
BMW 19,000.00 3/15/2005
I need to add the money colum to get 44,252.00. is that even possible from
a
text file?

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by youpak2000 | last post: by
10 posts views Thread by LuTHieR | last post: by
5 posts views Thread by lim4801 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.