|
-
Apr 25th, 2007, 06:28 AM
#1
Thread Starter
Addicted Member
[RESOLVED] [2005] Why is this not completeing
I'm reading from a file and finding the highest value with in the text file. For some odd reason this bit of code is stopping at the 54th number instead of transversing the entire 100 numbers. Any ideas why?
Code:
Dim SReader As System.IO.StreamReader
SReader = IO.File.OpenText("NumberSet")
intCount = 0
For intCount = 0 to SReader.Peek - 1
If CDbl(SReader.ReadLine.ToString) > NumHighest Then
NumHighest = CDbl(SReader.ReadLine.ToString)
End If
Next
SReader.Close()
Last edited by Abrium; Apr 30th, 2007 at 02:51 AM.
Reason: not resolved on issue
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 25th, 2007, 06:37 AM
#2
Re: [2005] Why is this not completeing
I've never used a loop like that with a streamReader. I'm not saying it can't be done but it's weird.
Try this and see if it works any better:
2 Code:
Dim numHighest As Double = 0
Dim SReader As New System.IO.StreamReader("C:\Temp\numberSet.txt")
Do While SReader.Peek <> -1
If CDbl(SReader.ReadLine) > numHighest Then
numHighest = CDbl(SReader.ReadLine)
End If
Loop
SReader.Close()
SReader.Dispose()
MessageBox.Show("Highest: " & numHighest.ToString)
-
Apr 25th, 2007, 07:03 AM
#3
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
you know I really need to perfect my methods of when to use what type of loop and this, that, and the other. I guess that is something that is only gained by experience... at least I hope. Thanks for the tip though Stim, let me punch that in and see what I get.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 02:27 AM
#4
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
What is the Reader.Dispose() doing for me I thought closing would remove it from memory. Let me go ahead and see if I can research that.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 02:35 AM
#5
Re: [2005] Why is this not completeing
In VB 2005 make use of the Using statement to dispose short-lived disposable objects. ALWAYS use a Using statement where you can:
vb Code:
Using sr As New StreamReader("file path here")
'Use sr here.
End Using 'sr gets implicitly disposed here.
-
Apr 26th, 2007, 02:37 AM
#6
Re: [2005] Why is this not completeing
Note also that neither Close nor Dispose actually remove anything from memory. Close implicitly calls Dispose and Dispose releases the file handle. The StreamReader object still exists in memory until the .NET garbage collector gets around to cleaning it up though.
-
Apr 26th, 2007, 03:33 AM
#7
Re: [2005] Why is this not completeing
Damn it, I keep forgetting that. I have started using 'Using' but learnt the other way first and old habits sometimes really do die hard.
-
Apr 26th, 2007, 04:57 AM
#8
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Don't feel bad Stim I'm still learning.. I'm not even crawling.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 05:32 AM
#9
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
That code in which you suggested is still stopping in the middle of my text file or just flat out not returning the highest number in the list.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 05:52 AM
#10
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Alright altered the code just a touch but still not returning the highest value. This is what it has morphed into:
Code:
using sreader as system.io.streamreader - file.opentext("numberset.txt)
for intcount = 0 to sreader.peek - 1
if numhighest < cdbl(sreader.readline) then
numhighest = cdbl(sreader.readline())
end if
next
end using
Very simple if statement that should fill the variable numhighest with the highest number in the file but it does not. The number it returns is at index 54. I have put in breaks and it seems that it just randomly changes. I wish I had a better explination.
This is just beyond me lol
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 05:52 AM
#11
Re: [2005] Why is this not completeing
Have you checked that your list only contains Double values and that there's no some extra character that can't be 'read'. Maybe you should use TryParse and see if that spots anything. Are there any spaces in the text file? Maybe that could be the reason.
Edit: Ah, you have added code. Now I see some reasons why there could be a problem.
Last edited by stimbo; Apr 26th, 2007 at 06:08 AM.
-
Apr 26th, 2007, 05:54 AM
#12
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Could you throw me a line of code using TryParse and I'll go check MSDN at the same time, their explinations are a bit over my skill level sometimes.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 05:54 AM
#13
Re: [2005] Why is this not completeing
Is this a typing mistake:
vb Code:
using sreader as system.io.streamreader - file.opentext("numberset.txt)
Why don't you just use as JMC posted:
vb Code:
Using sr As New StreamReader("file path here")
End Using
And why are you STILL using that FOR Loop?
-
Apr 26th, 2007, 06:05 AM
#14
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
I experimented with what JMC suggested and while I know he is all knowing I continued to get squiggly line after squiggly line telling me that my syntax was incorrect. I'll give it another whoorl. I'm all for trying new things. Give me a sec
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:09 AM
#15
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
And hey don't talk that way about my For loop, you don't know what we've been through together! hehe
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:09 AM
#16
Re: [2005] Why is this not completeing
2 Code:
Dim numHighest As Double = 0
Dim currentItem As Double = 0
Using sr As New IO.StreamReader("numberSet.txt")
Do Until sr.Peek = -1
If Double.TryParse(sr.ReadLine, currentItem) Then
If currentItem > numHighest Then
numHighest = currentItem
End If
End If
Loop
sr.Close() 'Examples on MSDN still had this line so thought I'd better use it
End Using
MessageBox.Show(numHighest.ToString)
-
Apr 26th, 2007, 06:15 AM
#17
Re: [2005] Why is this not completeing
You are aware that each time you are doing 'sreader.readline' a new line is being read right? Let's say the file contains the following lines:
4
3
7
1
Code:
Using sreader As New IO.StreamReader("file path here")
'first 'looping' / first line
For intcount = 0 To sreader.Peek - 1
If numhighest < CDbl(sreader.ReadLine) Then 'the first line contains 4
numhighest = CDbl(sreader.ReadLine()) 'numhighest becomes 3
End If
Next
'second 'looping' / third line
For intcount = 0 To sreader.Peek - 1
If numhighest < CDbl(sreader.ReadLine) Then 'the third line contains 7
numhighest = CDbl(sreader.ReadLine()) 'numhighest becomes 1
End If
Next
End Using
--------------
Edit: Stimbo beat me to it
Last edited by Half; Apr 26th, 2007 at 06:31 AM.
Reason: got beaten
VB 2005, Win Xp Pro sp2
-
Apr 26th, 2007, 06:26 AM
#18
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Thanks for asking Half, yes I am aware of what is happening. Each time the loop runs the next line should be read. If that amount read by readline() is larger then the current highest number then the amount in readline is transfered over to numhighest variable.
I have used many variations of this code and to be honest my Starting out with VB 3rd edition isn't covering ANY of the variations that are being listed by my forum surfing brethern. I'm not at my wits end, I find it rather funny at this point but its really making me, as Stim said, doubt the default txt file that is supplied to complete this exercise.
Feel me
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:27 AM
#19
Re: [2005] Why is this not completeing
Can you post the file? (Although I only doubted the file before I saw your code) 
And what should the highest number be?
I did try this and it did work on a small sample of number in a text file I created. (although I still wouldn't use a for loop)
vb Code:
Using sr As New IO.StreamReader("numberSet.txt")
For intcount As Integer = 0 To sr.Peek - 1
If numHighest < CDbl(sr.ReadLine) Then
numHighest = CDbl(sr.ReadLine())
End If
Next
sr.Close()
End Using
MessageBox.Show(numHighest.ToString)
Last edited by stimbo; Apr 26th, 2007 at 06:33 AM.
-
Apr 26th, 2007, 06:31 AM
#20
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
oh thats just wrong Stim lol give me a sec and I'll post highest number.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:38 AM
#21
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
999.4745 is the highest number in the list of all 100
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:46 AM
#22
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Ya know what lets not worry about it, I get off work in an hour and I'll go straight to the university and ride my professor's A$$ until she gives me a better explination or shows me that this is actually gonig to work. Enough people have put enough effort into this for the time being and I got a feeling that as much time that I have spent on it that when it snaps in its going to be burned into my head for LIFE.
Thanks for the effort though. Sometimes you just can't teach stupid
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 07:08 AM
#23
Re: [RESOLVED] [2005] Why is this not completeing
Post the file. I'm curious.
-
Apr 26th, 2007, 07:13 AM
#24
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] Why is this not completeing
you asked for it:
545.8267
188.4615
102.7812
270.6497
722.0612
440.2756
255.7861
336.2933
201.1559
224.9152
486.0958
142.6786
540.0567
184.0429
931.2226
89.76896
101.3734
542.329
979.6552
824.5981
506.505
748.0768
348.9839
6.895138
338.7644
363.8474
414.0257
324.7093
96.50375
207.0846
784.949
760.0351
396.239
740.74
135.3988
104.4377
425.7755
713.8981
280.6217
724.6772
647.3325
52.40126
505.5908
578.3342
750.6681
999.4745
143.1309
479.4346
856.2048
506.3507
402.0432
614.0745
968.154
872.8957
445.2306
306.0456
677.6412
638.2329
717.8224
32.45957
115.8309
721.3687
525.2997
689.3735
157.0518
811.8174
861.6199
107.415
871.9216
10.22573
487.342
817.4067
230.4719
718.9986
257.7518
65.99171
55.27354
782.9236
700.9865
81.25837
224.5615
879.7962
846.1599
588.3654
125.3596
161.0446
301.4871
575.1667
356.6058
142.3547
582.4102
307.8369
682.2772
844.2955
689.7227
596.4952
234.515
182.9435
937.6945
310.8318
Thats the list, sorry for all the headaches man.
I'll check back in tomorrow night after I have solved this "unknown" issue
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 07:17 AM
#25
Re: [RESOLVED] [2005] Why is this not completeing
I got the right answer with YOUR code (see Post #19 for version I used). LOL.
-
Apr 26th, 2007, 07:34 AM
#26
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] Why is this not completeing
you created your own file didn't you...
you recreated the problem didn't you....
hey question... "someone spit out their dummy" -- Is that UK slang? Sorry I had to ask.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 07:42 AM
#27
Re: [RESOLVED] [2005] Why is this not completeing
I can't re-create your problem though. You must have something else the matter with it or maybe it's just the fact it's just...... you.
Regarding the dummy thing, it's basically a phrase to indicate someone has had a childish tantrum. They throw or spit their dummy out (you know what a dummy is?)
-
Apr 26th, 2007, 07:47 AM
#28
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] Why is this not completeing
ohhh keep it up, don't make me come to the UK!
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 26th, 2007, 06:15 PM
#29
Re: [RESOLVED] [2005] Why is this not completeing
We went over this in one of your previous threads. You are misusing the Peek method. Peek is used to either get the value of the next character in the stream or tell you that there are no more characters. I cannot think of any possible situation where it would be appropriate to run a For loop to (Peek - 1). If that is your file contents then the first characters is '5'. If you open your StreamReader and then call Peek it will return the ASCII value of that character, which is 53. Thus your loop will run from 0 to (53-1) no matter how many lines there are in the file.
-
Apr 30th, 2007, 02:51 AM
#30
Thread Starter
Addicted Member
Re: [RESOLVED] [2005] Why is this not completeing
Now that I have been severly educated on the methods of a streamreader I acutally have a coding problem now. I think I have this coded to populate the variable with the highest number that is transversed in the list but my output doesn't seem to be giving me the highest number. Instead it returns the number at the end of this list which is 310.8318. See anything in this simple Do Until loop that would not make the highest number return in the NumHighest variable?
Code:
Using SReader As New IO.StreamReader(NumbersFile)
Do Until SReader.EndOfStream
If NumHighest < CDbl(SReader.ReadLine) Then
NumHighest = (SReader.ReadLine())
End If
Loop
End Using
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 30th, 2007, 02:53 AM
#31
Re: [2005] Why is this not completeing
Every time you call ReadLine you read a line, so what is this code going to do?
vb Code:
If NumHighest < CDbl(SReader.ReadLine) Then
NumHighest = (SReader.ReadLine())
-
Apr 30th, 2007, 02:57 AM
#32
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Everytime I call it its going to read the line. If the number in the line read is higher then numhighest it should populate the number that is read into the numhighest variable, I thought. Then again judging by your response in the form of a question I am guessing that my assumption is incorrect?
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 30th, 2007, 03:00 AM
#33
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
I know what your saying now but not really. I am going to loop until the EndOfStream so the last number that is going to be populated is the last one. I missed that, sorry. Let me see if I can code that a bit differently and take it easy i don't have the blessing quite yet as being able to read code as if it is being spelled out in english
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 30th, 2007, 03:31 AM
#34
Re: [2005] Why is this not completeing
That's not what I'm saying. I said that a line is read for each time you call ReadLine. That code is calling ReadLine twice so two lines will be read. You read a line and test the result against NumHighest. If it is greater than NumHighest you discard it, read another line and assign that to NumHighest. The fact that the previous line was higher says nothing about that line.
If you need to use the result of ReadLine more than once, which you do, then you need to assign it to a variable and then use that variable. You can then compare that variable to the current maximum and then assign THE SAME number to the NumHighest variable if it's appropriate.
-
Apr 30th, 2007, 04:02 AM
#35
Thread Starter
Addicted Member
Re: [2005] Why is this not completeing
Alright I changed my code to the following:
All I did was create a variable and give it the value of SReader.Readline so why now does it work?
Code:
Do Until SReader.EndOfStream
Dim Test As Double = CDbl(SReader.ReadLine)
If Test > NumHighest Then
NumHighest = Test
Its the same code with another variable and it works, either my mind is mush or I don't have as good of working knowledge as I thought about VB.
Abrium
Asking the beginners questions so you don't have to!
If by chance hell actually froze over and I some how helped you... Please rate.
-
Apr 30th, 2007, 04:08 AM
#36
Re: [2005] Why is this not completeing
It is NOT the same code because ReadLine is NOT a property. It is a method and it will return a different value each time it's called. As I have already explained, in your old code the value you were assigning to the NumHighest variable was NOT the same value you were comparing to it the line before. This new code only calls ReadLine once and uses the result for both the comparison and the assignment.
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
|