?
Solved

VB.Net - Text File Reading Previous Lines

Posted on 2015-01-13
3
Medium Priority
?
336 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 86

Accepted Solution

by:
Mike Tomlinson earned 1600 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 400 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

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 As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

770 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