Hello, I am reading a simple CSV text file into an array and trying to make a table that looks like:

Col 1 Col 2 Col 3 Total
Row 1 …….. …….. ……… ……..
Row2 …….. …….. ……… ……..
Total …….. …….. ……… ……..


The problem is I cannot figure out how to read the file into the array correctly.

My text file looks like:

1,2,3
4,5,6

Here is my code:

Module Module1

    Sub Main()
        Dim infile As IO.StreamReader = IO.File.OpenText("numbers.txt")
        Dim rows As Integer
        Dim cols As Integer
        Dim array(3, 2) As Integer


        Do While infile.Peek <> -1

            Dim line() As String = infile.ReadLine.Split(","c)

            For rows = 0 To 2
                For cols = 0 To 1
                    array(rows, cols) = line(rows)
                Next
            Next



        Loop

        For i As Integer = 0 To 2
            For d As Integer = 0 To 1
                Console.Write(array(i, d))
            Next
        Next


    End Sub

End Module

My output currently is 445566. Is the way I split the numbers wrong? Any suggestions or solutions?

I don't think peek is the correct method, you should something like

.readline

or

.readalltext

also your code is processing 3 rows and 2 cols but your data is 2 rows and 3 cols

And your output loop(s) are always going to just output one long string unless you add a Console.WriteLine between the first Next and the second Next.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.