MM007
May 20th, 2008, 09:29 AM
I am a VERY amatuer programmer (VB.NET 2003, Framework 2.0) who made an application that reads a text file, loads all the lines of that text file into an array, and randomly selects one line in that array to display it in a text box in the program.
However, I have had some issues with this code segment:
'Reading Race File for Length
ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
z1 = 0
Do Until ItemRead.Peek() = -1
monkey = ItemRead.ReadLine()
z1 = z1 + 1
Loop
ItemRead.Close()
'randomizing variable
Randomize()
Item = randomgen.Next(0, z1)
'Reading Race File into array
ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
z2 = 0
Do Until z2 = z1
Itemlist(z2) = ItemRead.ReadLine()
z2 = z2 + 1
Loop
ItemRead.Close()
The first thing this segment of code does is access the file, and read each line until the entire document is read through, assigning a number to the counter variable "z1" until the file is finished. The "monkey" variable does nothing whatsoever. I may remove it if it'll work without it.
Next, it picks a random integer between 0 and z1, which will be the line number displayed.
Lastly, it reads the file into an array called "Itemlist(z2)". I use z1 to define the limits of the array, but I get an out of bounds error when it tries to write the array. What am I missing?
Also, while I'm at it, is there an easier way to say "load each line into part of an array, and randomly pick one of those lines"? I've been having array dimension errors since I started on this program.
Also, I should note that when I have a file with 100 lines, this works without fail...but every time I try one with 120 lines, it crashes the application.
However, I have had some issues with this code segment:
'Reading Race File for Length
ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
z1 = 0
Do Until ItemRead.Peek() = -1
monkey = ItemRead.ReadLine()
z1 = z1 + 1
Loop
ItemRead.Close()
'randomizing variable
Randomize()
Item = randomgen.Next(0, z1)
'Reading Race File into array
ItemRead = IO.File.OpenText(Dir + "\Race\" + Race + ".txt")
z2 = 0
Do Until z2 = z1
Itemlist(z2) = ItemRead.ReadLine()
z2 = z2 + 1
Loop
ItemRead.Close()
The first thing this segment of code does is access the file, and read each line until the entire document is read through, assigning a number to the counter variable "z1" until the file is finished. The "monkey" variable does nothing whatsoever. I may remove it if it'll work without it.
Next, it picks a random integer between 0 and z1, which will be the line number displayed.
Lastly, it reads the file into an array called "Itemlist(z2)". I use z1 to define the limits of the array, but I get an out of bounds error when it tries to write the array. What am I missing?
Also, while I'm at it, is there an easier way to say "load each line into part of an array, and randomly pick one of those lines"? I've been having array dimension errors since I started on this program.
Also, I should note that when I have a file with 100 lines, this works without fail...but every time I try one with 120 lines, it crashes the application.