0

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?

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Reverend Jim
0

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

Edited by ChrisPadgham: n/a

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.