Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > Reading from text file and storing into an array


Reply
 
Thread Tools Display Modes
  #1  
Old 07-22-2012, 08:23 PM
DaBears57 DaBears57 is offline
Newcomer
 
Join Date: Jul 2012
Posts: 1
Default 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.
Reply With Quote
  #2  
Old 07-23-2012, 07:35 AM
AtmaWeapon's Avatar
AtmaWeapon AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,416
Default

You've got a logic error. What you want to have is this:

Code:
* Until the end of the file:
    * Read a line
    * If it's not a price:
        * Store the product ID
    * If it's a price:
        * Store the product price
        * Increment the index counter
        * Add the item to the listbox.
What you have is this:
Code:
* Until the end of the file:
    * Read a line
    * Try to convert the line to an integer
    * If the line was the number 5:
        * Store the string in the product information array
    * Otherwise:
        * Try to read it as a double and store that as the price
    * Add the product ID to the listbox
    * Increment the array counter
The problem here is that no line is ever going to be the number 5, so you never store a product ID, so you keep adding Nothing to the listbox over and over again. See if you can't figure out how to make what you've got look like what you should have.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->