Resolved Readline error?

yngasctmagfi

New member
Joined
Dec 7, 2009
Messages
3
Programming Experience
Beginner
First off, apologies if this is located in the wrong forum! I'm new to the community, and haven't quite familiarized myself with everything yet. Anywhoo,

I'm working on a small, simple program to take orders based on menu items and calculate the price. The menu is stored in a text file. When the form loads, it will read the text, sort the information, and display the items in a list box.

lstMenu.Items.Add(item.name & "(" & FormatCurrency(item.price) & ")")

Here is an example of some possible menu items, as would be found in the .txt:

Chicken Breast,2.99
Pizza Slice,2.50
Burger,1.75
Salad,1.90
Large Soda,1.20
Medium Soda,0.90


which should be displayed in the listbox as

Chicken Breast($2.99)
Pizza Slice($2.50)
Burger($1.75)
Salad($1.90)
Large Soda($1.20)
Medium Soda($0.90)


However, here is the problem. It only seems to be reading/outputting every other line from the file. So, instead of appearing as shown above, it is displayed such as this:

Pizza Slice($2.50)
Salad($1.90)
Medium Soda($0.90)

It seems like something very simple, but I can't figure out whats going wrong.. ?

As you will see, the line is split at the "," and put into the data() array, which is used to fill item's values. (name and price). Then item is added to an array of items. Finally, the menuItems is to be displayed in a list box.
VB.NET:
Public Class Form1
    [COLOR="Blue"]Dim[/COLOR] sr [COLOR="Blue"]As[/COLOR] IO.StreamReader
    [COLOR="Blue"]Dim [/COLOR]menuItem() [COLOR="Blue"]As[/COLOR] MenuItems

[COLOR="Blue"]Private Sub Load............
[/COLOR]
        [COLOR="Blue"]Dim[/COLOR] data() [COLOR="Blue"]As String[/COLOR]
        [COLOR="Blue"]Dim [/COLOR]inc [COLOR="Blue"]As Integer[/COLOR] = 0
        sr = IO.File.OpenText([COLOR="Red"]"MENU.txt"[/COLOR])
        [COLOR="Blue"]Do Until [/COLOR]sr.ReadLine = ""
            data = sr.ReadLine.Split([COLOR="Red"]","c[/COLOR])
            [COLOR="Blue"]Dim[/COLOR] item [COLOR="Blue"]As New[/COLOR] MenuItems()
            item.name = data(0)
            item.price = [COLOR="Blue"]CDbl[/COLOR](data(1))
            inc += 1
            [COLOR="Blue"]ReDim Preserve[/COLOR] menuItem(inc)
            menuItem(inc) = item
            data(0) = [COLOR="Blue"]Nothing[/COLOR]
            data(1) = [COLOR="Blue"]Nothing[/COLOR]
     [COLOR="Blue"]   Loop[/COLOR]

      [COLOR="Blue"]  For[/COLOR] i [COLOR="Blue"]As Integer[/COLOR] = 0 To inc
            lstMenu.Items.Add(menuItem(i).name & [COLOR="Red"]"("[/COLOR] & FormatCurrency(menuItem(i).price) &[COLOR="Red"]")"[/COLOR])
        [COLOR="Blue"]Next[/COLOR]
 
Last edited:
I created a textfile called Menu.txt with the following items in it:
Chicken Breast($2.99)
Pizza Slice($2.50)
Burger($1.75)
Salad($1.90)
Large Soda($1.20)
Medium Soda($0.90)

Then I ran this
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.DataSource = IO.File.ReadAllLines("d:\textfiles\menu.txt")
End Sub
And everything appeared just fine in my ListBox.
 
You are executing the 'ReadLine' method twice.

VB.NET:
 Do Until sr.ReadLine = ""    'ONCE
            data = sr.ReadLine.Split(","c)   'TWICE
            Dim item As New MenuItems()
            item.name = data(0)
            item.price = CDbl(data(1))
            inc += 1
            ReDim Preserve menuItem(inc)
            menuItem(inc) = item
            data(0) = Nothing
            data(1) = Nothing
        Loop
 
Ah ha!
I managed to sneak around the problem by using sr.peek -1 and changing how other parts of data were arranged in and out of the text file, however, I still had no idea what was wrong!

Thanks for the answer though! I would never have figured this out :)
 

Latest posts

Back
Top