|
-
Oct 26th, 2012, 10:52 PM
#1
Thread Starter
New Member
I need some help with loop and Streamreader
im using visual basic 2010 and using a streamreader to access a .txt file.
So I want to be able to open a .txtfile and extract certain information from it.
So far example, in the .txtfile it would say:
math
what is 2*3-9?
-3
math
what is 1*2*3?
6
chemistry
what is Ar?
Argon
What is O?
Oxygen
I'm trying to create a streamreader with a Do While Loop. So when I click the "Math" button, a math question would show up the textbox. and the user writes down an answer for that question.
Last edited by Joacim Andersson; Oct 27th, 2012 at 08:00 AM.
-
Oct 26th, 2012, 11:12 PM
#2
Re: I need some help with loop and Streamreader
Are you actually using VB6 or earlier? Me thinks not. All versions of VB from 7 (VB.NET 2002) are VB.NET, so your question belongs in the VB.NET forum. I've asked the mods to move it.
As for the question, you say that you're trying to write a Do While loop so show us what you've tried. Presumably you have some idea of what is supposed to happen inside that loop. Even if you can't write the code to do it, you must know what it's supposed to do. Please explain.
-
Oct 27th, 2012, 12:18 AM
#3
Thread Starter
New Member
Re: I need some help with loop and Streamreader
That's the problem. I know what "Loop" does with numbers. But I'm not sure how to use to to read lines using streamReader.
So far, this is what I was trying to write for my code. But I'm not even sure if themathbutton code makes sense.
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
OpenF.InitialDirectory = "C:/"
OpenF.FileName = "Open A File..."
OpenF.Filter = "ONLY Text Files (*.txt. | *.txt"
OpenF.ShowDialog()
End Sub
Private Sub btnMath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMath.Click
Dim myfile As System.IO.StreamReader
myfile = File.OpenText(myfilename)
Dim math As Integer
Do While myfile.Peek <> -1
myfile.ReadLine()
myfile.ReadLine()
Loop
myfile.Close()
Me.txtBox = Val(myfile.ReadLine)
End Sub
Basically, there are 3 lines repeating.
Math
What is 3*3?
9
Im trying to figure out a way to show the 2nd line on a textbox.
-
Oct 27th, 2012, 12:51 AM
#4
Re: I need some help with loop and Streamreader
 Originally Posted by Vince91889
That's the problem. I know what "Loop" does with numbers. But I'm not sure how to use to to read lines using streamReader.
That's not what I'm asking. I'm not asking you for code. I'm asking you to explain what the code is supposed to do. It's a rather important step that a lot of people ignore. If you try to write code without a clear understanding of what that code is supposed to do, what are the chances that it will do what it's supposed to? There's always going to be a certain amount of trial and error to get it to work perfectly but you can avoid a great many iterations if you have a clear idea of what it's supposed to do before you start. If you cant explain the steps that need to be performed, how can you write code to perform them?
-
Oct 27th, 2012, 01:08 AM
#5
Thread Starter
New Member
Re: I need some help with loop and Streamreader
Okay, so here is my understanding of what Loop does.
Do Until loop will loop until a condition is true.
So if I do,
Dim x as integer = 0
Do Until x = 10
x = x + 1
loop
It will show 1 2 3 4 5 6 7 8 9 10.
if I do,
Do until x = 10
x = x+5
loop
it would show 5, 10
If you think you have a better way of explaining the loop to me, please I'm all ears. I just cant interrelate the loop with streamReader.
-
Oct 27th, 2012, 05:40 AM
#6
Re: I need some help with loop and Streamreader
I think you may have misunderstood what JM was asking.
If you're reading data then you must be putting it somewhere (eg an Array, a List, a String). How you store the data will have an effect on the loop (if one is required at all).
A method to control the loop is to use the .EndOfStream property
eg
Code:
Do Until myReader.EndOfStream
myData = myReader.ReadLine ' Reads a single line of Data
.
.
Loop
or you could do away with the loop altogether
Code:
myData = myReader.ReadToEnd ' Reads the entire file of data into myData
myLines() = myData.Split(vbNewLine) ' Create an array with each element holding a line of data
There's probably a 1001 different ways of reading the data, the 'best' one is the one suited to how you're storing / processing the data once it's read.
-
Oct 27th, 2012, 07:58 AM
#7
Re: I need some help with loop and Streamreader
Thread moved to the VB.Net forum.
-
Oct 27th, 2012, 11:03 AM
#8
Re: I need some help with loop and Streamreader
You are risking getting frustrated over this one. A program has no concept of a file on disk; it can only work with what's loaded in its memory. So you constantly having to take care of that with all this peeking, advancing the stream reader, getting data form the line after the current one... it adds a layer of complexity which is useful if you are learning about Readers but not that important for loop logic.
The technique is useful for example when you have a 2 GB file which is hard to be loaded in memory in one go. But for your current setup why don't you try to load all the lines into a string array using your streamreader (and the loop you already have) and then loop through the array. When you already have all the lines, it is easier to get any line at any position relative to the mark.
-
Oct 27th, 2012, 07:10 PM
#9
Thread Starter
New Member
Re: I need some help with loop and Streamreader
So what is the simplest code to read specific lines on a .txt file??
I need to write a program where a user clicks a "math" or "chemistry" button and streamReader asks a question, then a writer tries to answer it by typing into a textbox. This is really confusing and hard for me.
-
Oct 27th, 2012, 07:20 PM
#10
Thread Starter
New Member
Re: I need some help with loop and Streamreader
This is what I'm trying to Build.
Design a computer educational system.
In the beginning, all buttons are disabled except the “Load Database” Button. The system uses the qandq.txt file to simulate a database. After the user clicks on “Load Database” button and chooses the qanda.txt file, all button are enabled, and the “Load Database” button will disappear.
The system first reads from the file qanda.txt. You decide how to store information from qanda.txt.
For example, you can store those information into either List, arrays, or invisible listboxes. The information includes Category, Question and Answer. Once all information from qanda.txt are read and stored, the system draws question randomly each time when the user clicks Math or Chemistry button.
When the Math button is clicked, the system randomly selects a question from the math category and shows the question in the multi-line textbox. Similarly, when the Chemistry button is clicked, the system randomly selects a question from chemistry category and shows the question in the multi-line textbox. (Random number Generator)
The user then types in an answer inside the single-line textbox. After the Submit Answer button is clicked, the system should check the user’s answer with the correct answer and provide the user with either one of the following message box depending whether the typed answer is correct.
If the user answers incorrectly, after the OK button is clicked, highlight the incorrect answer and wait for the user to answer again. If the user answers correctly, erase both question and answer text boxes so the user can click either Math or Chemistry button again to see the next question. You can set the form’s AcceptButton to the Submit answer button for user friendliness.
.
-
Oct 27th, 2012, 07:55 PM
#11
Re: I need some help with loop and Streamreader
You still haven't answered my question from earlier. What I was trying to get out of you is that, inside the Do loop, you need to read three lines of the file and store them as a single unit. The file obviously contains data in groups of three so you need to read it in groups of three. Your code from earlier has two ReadLine calls inside the loop, so it's not going to read the data correctly.
The best option here would be to create a type, i.e. a class or structure, to represent one record. Each record has three parts so your type would have three properties. You read the data in in groups of three, create an instance of your type, populate its properties with the data and then add it to a list. At the end of the loop, your list contains all the data grouped correctly.
If you want to select data at random then it's not really practical to read the file on demand. For a start, you don't know how many records there are until you've read the whole file, so how can you effectively choose a record at random? Doing anything random in .NET generally means creating an instance of the Random class and calling its Next to generate a random Integer, then using that in whatever way is appropriate. In your case, you would use it as an index into your list of items read from the file. If you have a single list containing all the items, you can filter that to get only the ones for the desired subject, then get a random one from that sub-list. Once you have that random item, get it's property values and display them in the UI.
-
Oct 27th, 2012, 08:37 PM
#12
Thread Starter
New Member
Re: I need some help with loop and Streamreader
Holy crap. You know your **** well. Damn, right now I'm just trying to figure out how to even show the questions in a textbox. Then I'd have to figure out the random number later.
Right now this is what I have so far. It probably doesnt make sense, but I'm still trying to figure out. Sorry guys, Im such a noob. I'm trying to learn through online class and it's terrible.
Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
If myfilename = "" Then
MsgBox("You need to click Load Database first", MsgBoxStyle.OkOnly)
Else
Dim myfile As StreamReader
myfile = File.OpenText(myfilename)
Dim r As String
Do While myfile.Peek <> -1
skiplines(2, myfile)
Select Case myfile.ReadLine
Case "Math"
r = myfile.ReadLine
End Select
Loop
myfile.Close()
Me.textboxShow.Text = Val(r)
End If
End Sub
Last edited by Vince91889; Oct 27th, 2012 at 08:56 PM.
-
Oct 27th, 2012, 08:57 PM
#13
Re: I need some help with loop and Streamreader
First things first, please use formatting tags to post code snippets in future to make them easier to read. You can use the Code or VBCode (Highlight) buttons on the advanced editor to add the tags or you can just type them yourself.
As for the code, the first thing I notice is that you are calling OpenText on a StreamReader, which isn't going to work. The StreamReader class doesn't have an OpenText member. The File class has an OpenText method that returns a StreamReader, but you're already calling that earlier.
Also, the first thing do in your Do loop is call 'skiplines', which I assume is going to read and discard two lines. Looking at your first post, the first two lines skipped will be "math" and "what is 2*3-9?". You then read the third line to see if it contains "Math", but that line is an answer, not a category. This is why I was trying to get you to explain, in plain English, what is supposed to happen inside the loop. The code you have doesn't do anything useful in this context and indicates that you still haven't formulated a clear idea of what's actually supposed to happen inside that loop. You know for a fact that you need to read three lines each iteration so surely the first thing to do would be to call ReadLine three times. You're not going to be skipping any lines in the final implementation so why write a 'skiplines' method at all? Just call ReadLine and don't do anything with the result if you want to ignore a line. Then you have the call there for later when you do want to use the result. Also, the category is the first of the three lines so surely you need to get that first result and test it to see whether it's a math question. If you had picked up a pen and paper and written out the steps that you need to perform inside that loop, surely that would be the first, e.g. "read first line and check whether it is 'math'".
Finally, you are assigning a value to 'r' inside the loop and then displaying the value of 'r' at the end of the loop. That means that 'r' will be assigned over and over and you will just see the last value.
-
Apr 10th, 2016, 10:30 AM
#14
New Member
Re: I need some help with loop and Streamreader
Hello Vince,
I have similar problem like yours. Were you able to figure it out.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|