Results 1 to 3 of 3

Thread: Reading from text file and storing into an array

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Reading from text file and storing into an array

    Hi, I am given a problem where I have a text file that contains the following:
    Code:
    12AVX
    5
    23ABC
    8.97
    23TWT
    4.69
    34ZAB
    12.5
    91BAN
    34.67
    Some of the instructions include "Define a structure named Product. The structure should contain two member variables: a String variable to store the item number and a Double variable to store the price" and "Declare a class-level array that contains five Product structure variables."

    I am stuck on the following "The form’s Load event procedure should read the item numbers and prices from the ItemInfo.txt fi le and store them in the classlevel array. It also should add the item numbers to the list box."

    Code:
    Option Explicit On
    Option Strict On
    Option Infer Off
    
    Public Class frmMain
    
        Private ProductInfo(4) As Product
    
        Structure Product
            Public strId As String
            Public dblPrice As Double
        End Structure
    
        Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            Dim inFile As IO.StreamReader
            Dim intArray As Integer
            Dim intLength As Integer
            Dim strTemp As String
    
            ' verify that the file exists
            If IO.File.Exists("ItemInfo.txt") = True Then
                ' open the file for input
                inFile = IO.File.OpenText("ItemInfo.txt")
                'process loop instructions until end of
    
                Do Until inFile.Peek = -1
                    strTemp = inFile.ReadLine
                    Integer.TryParse(strTemp, intLength)
    
                    If intLength = 5 Then
                        ProductInfo(intArray).strId = strTemp
                    Else
                        Double.TryParse(strTemp, ProductInfo(intArray).dblPrice)
                    End If
    
                    ' add the line to the list box
                    lstNumbers.Items.Add(ProductInfo(intArray).strId)
    
                    intArray = intArray + 1
                Loop
    
                ' close the file
                inFile.Close()
    
                ' select the first line in the list box
                lstNumbers.SelectedIndex = 0
    
            End If
        End Sub
    End Class
    When I run this code my list box is empty and I am not sure where I went wrong. Any help would be appreciated.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Reading from text file and storing into an array

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     ''' <summary>
    4.     ''' Product Structure
    5.     ''' </summary>
    6.     ''' <remarks>contains a string member + a double member.
    7.     ''' overriding the ToString, determines what is displayed in the listbox when you add a Product</remarks>
    8.     Private Structure Product
    9.         Dim itemNumber As String
    10.         Dim price As Double
    11.         Public Overrides Function ToString() As String
    12.             Return Me.itemNumber
    13.         End Function
    14.     End Structure
    15.     ''' <summary>
    16.     ''' products array
    17.     ''' </summary>
    18.     ''' <remarks>5 element array of Product</remarks>
    19.     Dim products(4) As Product
    20.  
    21.     ''' <summary>
    22.     ''' Form1_Load event
    23.     ''' </summary>
    24.     ''' <param name="sender"></param>
    25.     ''' <param name="e"></param>
    26.     ''' <remarks>here we read the text file, then loop through the lines + load the products array + add products to listbox</remarks>
    27.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    28.         Dim lines() As String = IO.File.ReadAllLines("fileName.txt")
    29.  
    30.         Dim counter As Integer = 0
    31.  
    32.         For x As Integer = 0 To lines.GetUpperBound(0) Step 2
    33.             products(counter).itemNumber = lines(x)
    34.             products(counter).price = CDbl(lines(x + 1))
    35.             ListBox1.Items.Add(products(counter))
    36.             counter += 1
    37.         Next
    38.  
    39.     End Sub
    40.  
    41.     ''' <summary>
    42.     ''' ListBox1_SelectedIndexChanged event
    43.     ''' </summary>
    44.     ''' <param name="sender"></param>
    45.     ''' <param name="e"></param>
    46.     ''' <remarks>this shows how you can use the ListBox.SelectedItem</remarks>
    47.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    48.         Label1.Text = DirectCast(ListBox1.SelectedItem, Product).price.ToString("c2")
    49.     End Sub
    50.  
    51. End Class

  3. #3
    Hyperactive Member
    Join Date
    Dec 2008
    Posts
    282

    Re: Reading from text file and storing into an array

    .paul.'s code should works fine..
    I'm still on the path of learning....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width