Problem with loops

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IanSD81
    New Member
    • Feb 2008
    • 6

    Problem with loops

    Hello, I have two problems I’m having trouble solving. The first involves a program that opens a text file with a bunch of random numbers out of order listed line by line. I have to list the Highest and the second Highest in a list box using loops and if statements. I had no trouble figuring out how to get the highest but how do I go about generating the second highest. The code is listed below.
    Code:
        Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
            Dim SR As IO.StreamReader = IO.File.OpenText("BID.TXT")
            Dim Bids As Double
            Dim Highest As Double = 0
            Dim SecondHighest As Double
    
            Do While (SR.Peek <> -1)
                Bids = CDbl(SR.ReadLine)
    
                If Bids > Highest Then
                    Highest = Bids
                End If
            Loop
            SR.Close()
    
    
    
            lstdisplay.Items.Add("The Highest Bid is: " & FormatCurrency(Highest))
    
    
    
    
        End Sub
    The second problem I am having is similar to the first but I have another text file that consists of a bunch names that are listed in Alphabetical order. Some of the names are repeated more than once. I have to use loops and if statements to display the file of names but for those that are repeated in the text file only list once. I have no clue how to go about doing this one. Any help would be great.
    Thanks.
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim SR As IO.StreamReader = IO.File.OpenText("NAMES.TXT")
            Dim Name As String
    
    
            Do While (SR.Peek <> -1)
                Name = SR.ReadLine
    
    
            Loop
    
    
    
        End Sub
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Check line 10[CODE=vb] Private Sub btnDisplay_Clic k(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnDisplay.Clic k
    Dim SR As IO.StreamReader = IO.File.OpenTex t("BID.TXT")
    Dim Bids As Double
    Dim Highest As Double = 0
    Dim SecondHighest As Double

    Do While (SR.Peek <> -1)
    Bids = CDbl(SR.ReadLin e)
    If Bids > Highest Then
    secondHighest = Highest 'Yeah!, thats it.
    Highest = Bids
    End If
    Loop
    SR.Close()
    lstdisplay.Item s.Add("The Highest Bid is: " & FormatCurrency( Highest))

    End Sub[/CODE]

    ^.^ For the other one, aplies the same thing.

    Comment

    • IanSD81
      New Member
      • Feb 2008
      • 6

      #3
      Hey thanks a lot!!!
      You defiantly helped me out.

      Comment

      Working...