Solved

VB.Net - Text File Reading Previous Lines

Posted on 2015-01-13
3
306 Views
Last Modified: 2015-01-14
Good Day Experts!

I have a new hot project here that I am having trouble finding a way to do one of the required functions.  

The text file has orders for a specific CustomerName and an OrderNumber on a line.  So, first I search for a specific CustomerName in the file iterating through the lines using a StringReader.  I find the CustomerName and grab the OrderNumber from the end of the line.  So, I iterate through more lines until I find the next occurrence of the specific CustomerName but the OrderNumber will be different.  When this happens, I need to back up one line. That line contains the dollar amount for the OrderNumber.

I am not sure how to back up a line in the StringReader or even if I need to use a different text file manipulation tool that gets previous lines.

Here is a skeleton sample of the data:

     Bob's Tree Farm          1234
          Product1
          Product2
          Product3
                               99.87
     Bob's Tree Farm          5678
           Product1
                                29.78
     Bob's Tree Farm          9012
           Product1
           Product2
           Product3
           Product4
                                79.99
         

Can you lend some insight?

Thanks,
0
Comment
Question by:Jimbo99999
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 400 total points
ID: 40546932
Just find your start line then create a new List(Of String):
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx

Now start reading lines and add each one to it until you reach the next order.  You've got all the products in the List now...

Air / Pseudo-code:

    Dim orderDetails As New List(Of String)

    If someCondition Then ' find the first line
        While Not SR.EndOfStream()
            line = SR.ReadLine()
            If Not "line is the next order" Then
                orderDetails.Add(line)
            Else
                Exit While
            End If
        End While

        ' ... do something with "orderDetails" ...

    End IF
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
ID: 40547846
You cannot back up with a StringReader, so you will have to record the last line as you go through.

Each time you read a line, store it in a variable.

Read the following line.

If that following line is your Customer, then the variable contains the dollar amount.

If that following line is not your Customer, then store it in the variable.
0
 

Author Closing Comment

by:Jimbo99999
ID: 40549210
Mike:

Your comment about the List got me thinking. I ended up putting each line in an ArrayList then iterated through the ArrayList.  That way I could "back-up" and get the previous line.

Thanks for the help,
jimbo99999
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question