VB.net (2008) streamreader help.

Newspapercrane

New Member
Messages
6
Reaction score
0
Points
0
I am currently working on a program which reads in a series of strings from a text file. Each line represents a clock in or clock out from an employee (Each employee has an individual text file).

Each line in the text file matches the following format exactly:

Code:
IN    08/28/2006        06:48

OUT   08/28/2006        11:54

IN    08/30/2006        06:54

OUT   08/30/2006        11:53
(note there is only one carriage return between each line)

My code is the following:

Code:
Imports System.IO

Public Class Form1
    Dim payPeriodStartDate As Integer
    Dim payPeriodEndDate As Integer
    Dim linePunchIn As String
    Dim linePunchOut As String
    Dim lineIn As String
    Dim punchInMonth As String
    Dim punchInDay As String
    Dim punchInYear As String
    Dim punchOutMonth As String
    Dim punchOutDay As String
    Dim punchOutYear As String
    Dim punchDirection As String
    Dim punchInHour As Integer
    Dim punchInMinute As Integer
    Dim punchOutHour As Integer
    Dim punchOutMinute As Integer
    Dim punchInTime As Single 'This variable holds the time in hundreths of minutes format
    Dim punchOutTime As Single 'Ditto
    Dim shiftHours As Single








    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If payPeriodStartDay.Text = "" Then 'This block of code checks to make sure that the day month and year of the pay period start are set.
            MsgBox("Please Select a pay period start day.")
        ElseIf payPeriodStartMonth.Text = "" Then
            MsgBox("Please Select a pay period start Month.")
        ElseIf payPeriodStartYear.Text = "" Then
            MsgBox("please Select a Pay period Start Year.")
        Else
            payPeriodStartDate = payPeriodStartYear.Text() & payPeriodStartMonth.Text & payPeriodStartDay.Text 'Adds the day month and year of the pay period start date into an integer
        End If

        If payPeriodEndDay.Text = "" Then 'This block of code checks to make sure that the day month and year of the pay period start are set.
            MsgBox("Please Select a pay period end day.")
        ElseIf payPeriodEndMonth.Text = "" Then
            MsgBox("Please Select a pay period End Month.")
        ElseIf payPeriodEndYear.Text = "" Then
            MsgBox("please Select a Pay period End Year.")
        Else
            payPeriodEndDate = payPeriodEndYear.Text() & payPeriodEndMonth.Text & payPeriodEndDay.Text 'Adds the day month and year of the pay period end date into an integer
        End If

        If File.Exists(OpenFileDialog1.FileName) = False Then 'checks to make sure the file exists before opening it.
            MsgBox("File does not exist!")
            Exit Sub
        End If

        Dim ioFile As New StreamReader(OpenFileDialog1.FileName) 'opens the file



        ' While ioFile.EndOfStream <> True




        While ioFile.EndOfStream <> True
            lineIn = ioFile.ReadLine()
            ListBox1.Items.Add(lineIn)





            punchDirection = lineIn.Substring(0, 2)

            If punchDirection = "IN" Then
                punchInMonth = lineIn.Substring(5, 3)
                punchInDay = lineIn.Substring(9, 2)
                punchInYear = lineIn.Substring(12, 4)
                punchInHour = lineIn.Substring(24, 2)
                punchInMinute = lineIn.Substring(27, 2)
                punchInTime = punchInHour + (punchInMinute / 60)

            ElseIf punchDirection = "OU" Then

                punchOutMonth = lineIn.Substring(5, 3)
                punchOutDay = lineIn.Substring(9, 2)
                punchOutYear = lineIn.Substring(12, 4)
                punchOutHour = lineIn.Substring(24, 2)
                punchOutMinute = lineIn.Substring(27, 2)
                punchOutTime = punchOutHour + (punchOutMinute / 60)

                shiftHours = punchOutTime - punchInTime
                txtOutput.Text = shiftHours
            End If

            ListBox1.Items.Add(lineIn)


        End While


        ioFile.Close() 'closes the file


    End Sub


    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        OpenFileDialog1.ShowDialog()
        txtFileLocation.Text = OpenFileDialog1.FileName







    End Sub
End Class

So the problem I'm running into is this: On the while loop, the second time through for some reason it's not reading in the line from the text file, causing it to crash when I attempt to retrieve "punchDirection" from the substring (since I can't read from the string array because there's nothing there).

Any suggestions and/or guidance you can give me would be greatly appreciated!
Thank you for your time.

---------- Post added at 12:09 PM ---------- Previous post was at 10:31 AM ----------

I figured it out, I needed another readline statement at the end of the file.
 
Top