I am writing a program that manipulates text file using console application. What I want to do is to delete 1 specific line based on the ID entered by the user.

For example:

DR-01|Coke|25.80
DR-02|Sprite|25.80
DR-03|Sarsi|25.80
DR-04|Coke|25.80

When the user entered DR-02, the result would be like this:

DR-01|Coke|25.80
DR-03|Sarsi|25.80
DR-04|Coke|25.80

My program should delete the line with the ID DR-02, but my program deletes items from DR-02 to DR-04 instead of DR-02 only.

My code:

Module Mod1

Dim txtDrink As String = Application.StartupPath & ("\drink.txt")

sub main
    CountDrinkIdToDelete
end sub

Sub CountDrinkIdToDelete()
        Dim lines As New List(Of String)
        Dim inp As String
        Dim intNum As Integer

        ViewDrinkRecord()

        Console.Write("Enter Dessert ID: ")
        inp = Console.ReadLine.ToUpper.Trim

        If inp.Length = "5" Then
            Call DrinkTxtFile()

            Using reader As StreamReader = New StreamReader(txtDrink)
                Do While (True)
                    Dim line As String = reader.ReadLine
                    lines.Add(line)

                    If line Is Nothing Then
                        Exit Do
                    End If
                    intNum += 1

                    If Left(line, 5).Contains(inp) Then
                        lines.RemoveAt(intNum - 1) ' this line removes the item
                        Exit Do
                    End If
                Loop
            End Using
        End If

        Using sw As New StreamWriter(txtDrink)
            For Each line As String In lines
                sw.WriteLine(line)
            Next
        End Using
        Console.WriteLine("Successful")
    End Sub


    Sub DrinkTxtFile()
        If File.Exists(txtDrink) = False Then
            File.Create(txtDrink).Dispose()
        End If
    End Sub

End Module

Feel free to share your ideas. Thanks in advance,, :)

Almost.
Instead of adding it to the list and then deleting it from the list,
either:
1) Check it before it goes into the list and DON'T ADD it if it matches the ID.
2) Add all to the list and just DON'T OUTPUT it to the new file if it matches the ID.

Also, you can use StartsWith rather than calling Left and Contains.

try this one kathlyn.:D

Imports System.Windows.Forms
Imports System.IO
Module Module1

    Dim txtDrink As String = Application.StartupPath & ("\drink.txt")

    Sub main()
        CountDrinkIdToDelete()
    End Sub

    Sub CountDrinkIdToDelete()
        Dim lines As New ArrayList
        Dim inp As String
        Dim linya As String
        Dim reader As StreamReader

        Call DrinkTxtFile()

        Console.Write("Enter Dessert ID: ")
        inp = Console.ReadLine.ToUpper.Trim

        reader = File.OpenText(txtDrink)
        While reader.Peek <> -1

            linya = reader.ReadLine()
            If Not linya.Contains(inp) Then
                lines.Add(linya) 'temporary storage for all data that is not equal to inp
            End If

        End While
        reader.Close()

        If File.Exists(txtDrink) Then  'delete the whole data in textfile
            File.Delete(txtDrink)
        End If

        Dim objWriter As New System.IO.StreamWriter(txtDrink, True)
        For Each item As String In lines
            objWriter.WriteLine(item)
        Next
        objWriter.Flush()
        objWriter.Close()
        Console.WriteLine("Successful")
    End Sub
    Sub DrinkTxtFile()
        If File.Exists(txtDrink) = False Then
            File.Create(txtDrink).Dispose()
        End If
    End Sub
End Module
This question has already been answered. Start a new discussion instead.