Solved

VB.Net - Text File Reading Previous Lines

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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