Results 1 to 36 of 36

Thread: [RESOLVED] [2005] Why is this not completeing

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Resolved [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.

  2. #2
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Dim numHighest As Double = 0
    2.         Dim SReader As New System.IO.StreamReader("C:\Temp\numberSet.txt")
    3.  
    4.         Do While SReader.Peek <> -1
    5.             If CDbl(SReader.ReadLine) > numHighest Then
    6.                 numHighest = CDbl(SReader.ReadLine)
    7.             End If
    8.         Loop
    9.         SReader.Close()
    10.         SReader.Dispose()
    11.  
    12.         MessageBox.Show("Highest: " & numHighest.ToString)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Using sr As New StreamReader("file path here")
    2.     'Use sr here.
    3. End Using 'sr gets implicitly disposed here.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  8. #8

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  9. #9

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  10. #10

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Question 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.

  11. #11
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  12. #12

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  13. #13
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Why is this not completeing

    Is this a typing mistake:

    vb Code:
    1. using sreader as system.io.streamreader - file.opentext("numberset.txt)

    Why don't you just use as JMC posted:
    vb Code:
    1. Using sr As New StreamReader("file path here")
    2.  
    3.  
    4. End Using

    And why are you STILL using that FOR Loop?
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  14. #14

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  15. #15

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  16. #16
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Why is this not completeing

    2 Code:
    1. Dim numHighest As Double = 0
    2.   Dim currentItem As Double = 0
    3.  
    4.         Using sr As New IO.StreamReader("numberSet.txt")
    5.             Do Until sr.Peek = -1
    6.                 If Double.TryParse(sr.ReadLine, currentItem) Then
    7.                     If currentItem > numHighest Then
    8.                         numHighest = currentItem
    9.                     End If
    10.                 End If
    11.             Loop
    12.             sr.Close() 'Examples on MSDN still had this line so thought I'd better use it
    13.         End Using
    14.  
    15.         MessageBox.Show(numHighest.ToString)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  17. #17
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    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

  18. #18

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  19. #19
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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:
    1. Using sr As New IO.StreamReader("numberSet.txt")
    2.             For intcount As Integer = 0 To sr.Peek - 1
    3.                 If numHighest < CDbl(sr.ReadLine) Then
    4.                     numHighest = CDbl(sr.ReadLine())
    5.                 End If
    6.             Next
    7.             sr.Close()
    8.         End Using
    9.  
    10.         MessageBox.Show(numHighest.ToString)
    Last edited by stimbo; Apr 26th, 2007 at 06:33 AM.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  20. #20

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  21. #21

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  22. #22

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  23. #23
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] [2005] Why is this not completeing

    Post the file. I'm curious.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  24. #24

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  25. #25
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [RESOLVED] [2005] Why is this not completeing

    I got the right answer with YOUR code (see Post #19 for version I used). LOL.
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  26. #26

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  27. #27
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    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?)
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  28. #28

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  30. #30

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  31. #31
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. If NumHighest < CDbl(SReader.ReadLine) Then
    2.     NumHighest = (SReader.ReadLine())
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  32. #32

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  33. #33

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  34. #34
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  35. #35

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.

  36. #36
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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