By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,039 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,039 IT Pros & Developers. It's quick & easy.

Problem with loops

P: 6
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.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  2.         Dim SR As IO.StreamReader = IO.File.OpenText("BID.TXT")
  3.         Dim Bids As Double
  4.         Dim Highest As Double = 0
  5.         Dim SecondHighest As Double
  6.  
  7.         Do While (SR.Peek <> -1)
  8.             Bids = CDbl(SR.ReadLine)
  9.  
  10.             If Bids > Highest Then
  11.                 Highest = Bids
  12.             End If
  13.         Loop
  14.         SR.Close()
  15.  
  16.  
  17.  
  18.         lstdisplay.Items.Add("The Highest Bid is: " & FormatCurrency(Highest))
  19.  
  20.  
  21.  
  22.  
  23.     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.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim SR As IO.StreamReader = IO.File.OpenText("NAMES.TXT")
  3.         Dim Name As String
  4.  
  5.  
  6.         Do While (SR.Peek <> -1)
  7.             Name = SR.ReadLine
  8.  
  9.  
  10.         Loop
  11.  
  12.  
  13.  
  14.     End Sub
May 2 '08 #1
Share this Question
Share on Google+
2 Replies


kadghar
Expert 100+
P: 1,295
Check line 10
Expand|Select|Wrap|Line Numbers
  1.     Private Sub btnDisplay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
  2.         Dim SR As IO.StreamReader = IO.File.OpenText("BID.TXT")
  3.         Dim Bids As Double
  4.         Dim Highest As Double = 0
  5.         Dim SecondHighest As Double
  6.  
  7.         Do While (SR.Peek <> -1)
  8.             Bids = CDbl(SR.ReadLine)
  9.             If Bids > Highest Then
  10.                 secondHighest = Highest 'Yeah!, thats it.
  11.                 Highest = Bids
  12.             End If
  13.         Loop
  14.         SR.Close()
  15.         lstdisplay.Items.Add("The Highest Bid is: " & FormatCurrency(Highest))
  16.  
  17.     End Sub
^.^ For the other one, aplies the same thing.
May 2 '08 #2

P: 6
Hey thanks a lot!!!
You defiantly helped me out.
May 3 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.