Solved

VB.Net - Text File Reading Previous Lines

Posted on 2015-01-13
3
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 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