Results 1 to 6 of 6

Thread: VB 2005 Reading input files

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    1

    VB 2005 Reading input files

    I am a noob to VB and have tried messing with this code for hours now.

    Code:
    Private Sub btnPres_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPres.Click
            Dim strFileName As String = CurDir() & "\USPres.TXT"
            Dim sr As IO.StreamReader = New IO.StreamReader(strFileName)
    
            Dim strPres As String
            Dim intCounter, intNum As Integer
    
            intNum = CInt(txtInput.Text)
    
    
            intCounter = 1
            If intNum > 44 Then
                MessageBox.Show("you need to type an integer between 1 and 44 inclusice.", "U.S. Presidents", MessageBoxButtons.OK)
            End If
            Do While sr.Peek <> -1
                strPres = CStr(sr.ReadLine)
            Loop
            sr.Close()
    
            If sr.Peek = 0 Then
                strPres = CStr(sr.ReadLine)
                If intNum = intCounter Then
                    intCounter = intCounter + 1
                Else : txtOutput.Text = strPres & " Was President number " & txtInput.Text
                End If
            End If
    
        End Sub
    How do I get the line of the file to correspond with the number that is inputted in the text box? The file looks like this...

    George Washington
    John Adams
    Thomas Jefferson
    James Madison
    James Monroe
    John Quincy Adams
    Andrew Jackson
    Martin Van Buren
    William Henry Harrison
    ect....

    And the program looks like this...




    please help guys...

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: VB 2005 Reading input files

    Thread moved from 'VB6 and Earlier' forum to 'VB.Net' (VB2002 and later) forum

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: VB 2005 Reading input files

    You are lacking a lot of structure there - do you understand how the program execution will flow through the routine.

    For a start if the user enters a number outside of 1-44 they will get a message but it will then proceed and try and match it, but I would have thought you'd want to bail out from there.

    Then you loop through and read the data from the text box via a streamreader until Peek returns -1

    Then you close your streamreader.

    Then you call Peek again on the streamreader that you've just closed.

    You are also incrementing your counter only if it = the number you're looking for when surely it should be the other way around.

    Have you put a breakpoint on the first line and stepped through your routine - that will help you understand where it is going.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB 2005 Reading input files

    i'd recommend using a numericupdown control for the number. set minimum to 1, + maximum to 44.

    then the code would be something like this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim presidents() As String = {"George Washington", "John Adams", "Thomas Jefferson", "James Madison", _
    4.                                   "James Monroe", "John Quincy Adams", "Andrew Jackson", "Martin Van Buren", _
    5.                                   "William Henry Harrison", "John Tyler", "James K. Polk", "Zachary Taylor", _
    6.                                   "Millard Fillmore", "Franklin Pierce", "James Buchanan", "Abraham Lincoln", _
    7.                                   "Andrew Johnson", "Ulysses S. Grant", "Rutherford B. Hayes", "James A. Garfield", _
    8.                                   "Chester A. Arthur", "Grover Cleveland", "Benjamin Harrison", "Grover Cleveland", _
    9.                                   "William McKinley", "Theodore Roosevelt", "William Howard Taft", "Woodrow Wilson", _
    10.                                   "Warren G. Harding", "Calvin Coolidge", "Herbert Hoover", "Franklin D. Roosevelt", _
    11.                                   "Harry S. Truman", "Dwight D. Eisenhower", "John F. Kennedy", "Lyndon B. Johnson", _
    12.                                   "Richard Nixon", "Gerald Ford", "Jimmy Carter", "Ronald Reagan", "George H. W. Bush", _
    13.                                   "Bill Clinton", "George W. Bush", "Barack Obama"}
    14.  
    15.     Private Sub NumericUpDown1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles NumericUpDown1.KeyDown
    16.         e.Handled = True 'don't allow keyboard input
    17.     End Sub
    18.  
    19.     Private Sub NumericUpDown1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles NumericUpDown1.KeyPress
    20.         e.Handled = True 'don't allow keyboard input
    21.     End Sub
    22.  
    23.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    24.         TextBox1.Text = "The " & addSuffix(NumericUpDown1.Value) & " President was:" & Environment.NewLine & Environment.NewLine & presidents(NumericUpDown1.Value - 1)
    25.     End Sub
    26.  
    27.     Private Function addSuffix(ByVal number As Integer) As String
    28.         Select Case number
    29.             Case 1, 21, 31, 41
    30.                 Return number.ToString & "st"
    31.             Case 2, 22, 32, 42
    32.                 Return number.ToString & "nd"
    33.             Case 3, 23, 33, 43
    34.                 Return number.ToString & "rd"
    35.             Case Else
    36.                 Return number.ToString & "th"
    37.         End Select
    38.     End Function
    39.  
    40. End Class
    Attached Images Attached Images  
    Last edited by .paul.; Apr 28th, 2009 at 10:27 AM. Reason: screenshot added:

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    Re: VB 2005 Reading input files

    I thought Barrack Obama is the 44th president. I don't know why someone would say he was
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: VB 2005 Reading input files

    yeah i noticed that when i posted. must be getting lazy in my old age
    least it leaves I LastWish I something to work on.

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