I am writing a program for my father so he can better track how much petrol he uses. Basically it grabs a line from a .txt file and splits it into 3 values which are used to perform basic calculations with. The calculations and values are then chucked into a Listview for easy viewing. The problem as you may have deduced from the tile is that my program crashes at line 31 of the code, after it reads all the data in the .txt file it tries to assign tempstring2 a line that does not exist. It then spits out a System.IO.IOException
error message. I've tried to write more code to prevent it, but to no avail. Any help would be greatly appreciated. This is the format of the .txt file.
Edit: Now that I know what the problem is, I want to compare two dates using DateDiff
but I can't compare the 2nd date to the 1st because I don't know how to read the next date of the .txt file without doing another LineInput
. I basically want to compare Sat 11 Jan 2014
with Thu 23 Jan 2014
in one loop and with one LineInput
.
Sat 11 Jan 2014,446.0,32.50
Thu 23 Jan 2014,566.2,42.08
Wed 05 Feb 2014,535.4,39.27
Sun 16 Feb 2014,486.2,36.10
Thu 27 Feb 2014,536.4,41.36
etc...
This is my new code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnTest.Click
Dim filenum As Integer = FreeFile()
Dim tempstring As String
Dim temp As String
Dim temparray(2) As String
Dim date1, date2 As Date
Dim date3 As String
FileOpen(filenum, "Stats.txt", OpenMode.Input)
While Not EOF(filenum)
tempstring = LineInput(filenum)
temp = tempstring
temparray = Split(temp, ",", -1)
date1 = temparray(0)
date3 = DateDiff(DateInterval.Day, date2, date1)
Dim stats1 As New ListViewItem(temparray(0), 0)
stats1.SubItems.Add(temparray(1))
stats1.SubItems.Add(temparray(2))
stats1.SubItems.Add(FormatNumber(temparray(2) / (temparray(1) / 100), 2))
stats1.SubItems.Add(FormatNumber(temparray(1) / temparray(2), 2))
stats1.SubItems.Add(FormatNumber(temparray(1) / temparray(2) * 50, 2))
stats1.SubItems.Add(date3)
lvwStats.Items.Add(stats1)
For i As Integer = 0 To lvwStats.Columns.Count - 1
lvwStats.Columns(i).Width = -2
Next i
End While
End Sub
End Class