Results 1 to 6 of 6

Thread: Open a text file, read a random line, add to listbox, rinse, repeat X times

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Open a text file, read a random line, add to listbox, rinse, repeat X times

    Hello, this is an old program I am building to read a text file of names, randomize them, and add them to a listbox. I've got it randomizing things properly,but it's not quite working properly. It reads a few names from the file, and then fills the rest with blank space. There are 24 entries in the text file, and it does add 24 entries to the listobx, but most of them are blank spaces. What I think I need to do is get a random number, read the next line of the file that number of times, put that into the listbox, and then reset the position to the top, get another random number, do it again. I feel like my code should work, but doesn't for some reason. I read over this thread, but i can't quite tell what's going on there. This is probably a pretty simple question or just something I've overlooked.

    Code:
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim x As Integer = 0
            Dim sr As StreamReader = New StreamReader(txtfile.Text)
            Do While sr.Peek() >= 0
                sr.ReadLine()
                x = x + 1
            Loop
            sr.Close()
            txtnumber.Text = ("Wow! " & x & " people!")
            Dim y As Integer = x
            Dim v As Integer
            Dim name As StreamReader = New StreamReader(txtfile.Text)
            Dim s As String
            Dim t As Integer = 0
            Do While y > 1
                Dim z As System.Random = New System.Random()
                v = z.Next(1, x) 'Get a new random number, call it V
                Do While v > 0
    
                    s = (name.ReadLine())
                    v = v - 1
                    
                Loop
                ListBox1.Items.Add(New String(s))
                t = t + 1
                Label2.Text = (t)
                TextBox1.Text = (s)
    
                y = y - 1
            Loop
    
        End Sub

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Open a text file, read a random line, add to listbox, rinse, repeat X times

    I think that it would be easier to read the entire textfile, store the contents of that textfile into an array or list(of string), and then select random name from that array or list(of string) to add to the listbox. I would also do it without the Do loops, just out of personal preferance. In the example below, I assume that the delimiater is a new line:
    Code:
    Dim sr As New IO.StreamReader("c:\text file.txt")
            Dim fileContents As String = sr.ReadToEnd
            Dim fileArr() As String = fileContents.Split(CChar(Environment.NewLine))
    
            'X = to the amount of times you want to add a random name
            Dim x As Integer = 3
            For i As Integer = 0 To x
                Dim randItem As Integer = rand.Next(0, fileArr.Count)
                ListBox1.Items.Add(fileArr(randItem))
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Re: Open a text file, read a random line, add to listbox, rinse, repeat X times

    Wow! Thanks for the quick reply! Anyway, i'm kind of new and i have never done anything with arrays, i do know that they would probably be easier to use though.

    Using your code, if i made X 24, it would print out 24 randomized names, right? And also, what if i wanted to email the people on the list? would i use the FileArr(randitem) bit like you did for the listbox?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Open a text file, read a random line, add to listbox, rinse, repeat X times

    Using your code, if i made X 24, it would print out 24 randomized names, right? And also, what if i wanted to email the people on the list? would i use the FileArr(randitem) bit like you did for the listbox?
    Nope if "x" was 24 then 25 names would be printed out. Reason being is I start my counter at 0. If you want 24 names, then set x = 23. Emailing the list, is beyond me. I tried emailing in vb.net before and just got frustrated, but I'm sure somebody else could help you out. The FileArr(randitem) simply gets a random item from the file.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Feb 2013
    Posts
    3

    Re: Open a text file, read a random line, add to listbox, rinse, repeat X times

    I actually managed to get emailing to work long ago when i was taking a class for this. It's actually pretty easy, provided you are willing to put the password and username of your email in your program. Also, How would i go about skipping the name if it's already appeared on the list? so that all 24 names get used, and none are duplicates? i'd imagine it would involve messing with arrays again, but i'm unfamiliar with how they work. I suppose I could just add the name to a string and compare the new random name with the names in the string, add 1 to x (I guess?) and then start over?

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Open a text file, read a random line, add to listbox, rinse, repeat X times

    Sorry I'm just getting back to you, the day after Mardi Gras hasn't been good to me. Back to the question, an array is a fixed size. To 'remove an item' from an array you have to redeclare that array with the items you want. However, rather than doing that I would suggest using a General List(of T), more specifically a List(Of String). With list you're able to remove items quite easily. Here is an example of what you're wanting to do with a List(of T):

    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim rand As New Random
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If IO.File.Exists("c:\test file.txt") Then
                Dim sr As New IO.StreamReader("c:\test file.txt")
                Dim fileContents As String = sr.ReadToEnd
                Dim fileList As New List(Of String)
    
                'Stores the contents of fileContents as items 
                'where the delimiator is a new line
                'I use vbCrLf in this case instead of environment.newline
                'Because environment.newline adds a blank space
                For Each Str As String In fileContents.Split(CChar(vbCrLf))
                    fileList.Add(Str)
                Next
    
                'X = to the amount of times you want to add a random name
                Dim x As Integer = 3
                For i As Integer = 0 To x
                    'Gets a random number from 0 to the fileList's item count
                    Dim randItem As Integer = rand.Next(0, fileList.Count)
                    'Adds the item to the listbox
                    ListBox1.Items.Add(fileList.Item(randItem))
                    'Removes the item from the list
                    fileList.RemoveAt(randItem)
                Next
            Else
                MessageBox.Show("File Not Found")
            End If
        End Sub
    End Class
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

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