Solved

VB.Net - Text File Reading Previous Lines

Posted on 2015-01-13
3
302 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now