Guest tikki Posted May 23, 2002 Posted May 23, 2002 Greetings, I am working on a small program to track the debt my brother is in to me (he borrows a lot of money ;)). The data file is a basic txt file of with a format like this. Gasoline $-20.00 Food $-5.00 Washed my car $+10.00 I am reading in the txt file using StreamReader on form load and wrote a sub procedure called GetDollarAmounts() which uses a for loop to go through each character and look for the $ sign using SubString. When it finds that dollar sign, it then reads in the appropriate sign and Substrings to the first number and does a do...while to get all numbers (via an IsNumeric function I wrote). It finally puts the dollar amount in a totalAmount var which it displays on a label at the bottom of my form. That is a mess that doesn't work. Is there an easier way to accomplish this? Any help is appreciated. Quote
Spike Posted May 23, 2002 Posted May 23, 2002 It looks like each line of the text file is separted by a vbCrLf....if that is the case, you can use the substring to find the "$" sign, and then start the next substring were the dollar sign is, and then search for vbCrLf. Dim a As Integer Dim b As Integer Dim s As String = "mydata $1.25" & vbCrLf Dim sOutput As String a = s.IndexOf("$") b = s.IndexOf(vbCrLf, a) sOutput = sOutput.Substring(a, b - a) *Note, it may need some tweaking, because i didn't test it. Quote
Guest tikki Posted May 25, 2002 Posted May 25, 2002 I don't quite understand how I can get this to work when my data.txt file will be so dynamic. In your example you have a hardcoded string to display one of the items in the list.... maybe i am too narrowminded, or maybe it is just too late in the evening for me :) Quote
Spike Posted May 25, 2002 Posted May 25, 2002 Well, that was just a small example. You should load the file into a string and loop through the string and pull out the information similar to the example. Quote
Guest tikki Posted May 25, 2002 Posted May 25, 2002 yeah, i realized that this morning. Amazing what sleep deprivation does to ya ;) I am working on it now. I'll post my results a bit later tonight hopefully. Thanks. Quote
Guest tikki Posted May 26, 2002 Posted May 26, 2002 Fixed my problems! Thanks to all. Was a learning experience since I am terrible when it comes to string manipulation. Posted my code in case anyone else ever happens to run into my problem and wants some reference :) Sub GetDollarAmounts() Dim StreamToDisplay As StreamReader Dim strCurrentLine As String Dim intIndexOfDollar As Integer 'index of $ in each line Dim intIndexOfReturn As Integer 'index of vbCrLf Dim dblDollarAmount As Double 'total dollar amount Dim dblTotalDollarAmount As Double StreamToDisplay = File.OpenText("C:\data.txt") ' Do this while not at the end of the file. Do While StreamToDisplay.Peek <> -1 strCurrentLine = StreamToDisplay.ReadLine & vbCrLf intIndexOfDollar = strCurrentLine.IndexOf("$") intIndexOfReturn = strCurrentLine.IndexOf(vbCrLf, intIndexOfDollar) dblDollarAmount = strCurrentLine.Substring(intIndexOfDollar, intIndexOfReturn - intIndexOfDollar) dblTotalDollarAmount += dblDollarAmount Loop lblTotalDebt.Text = dblTotalDollarAmount End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.