8 Replies - 3211 Views - Last Post: 09 December 2012 - 01:50 AM Rate Topic: -----

#1 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Text File to Array, Line by Line

Posted 08 December 2012 - 03:09 AM

So I can already tell that I'm beginning to struggle- looking at code for too long. So figured I'd post this now as it's late already, may get a lucky post or 2 by tomorrow morning.


I have a txt file, example:
"12,34,56
78,91,23
45,67,89
12,34,45
67,89,10"


You get the point- it contains sets of 3 digits followed by line breaks. I need to split the contents line by line and place into an array(2d) for usage further along. I don't know if it's my memory or what, but the pieces ain't fitting. I really cannot stand arrays sometimes. I've gotten the entire file contents to display, but all the loops I've tried have shown the digits 1-by-1 instead of line-by-line... any hints/ideas? Any advice much appreciated, guys.

Here is my current code, I've been playing around for a while now. I've cleaned it up for the sake of posting it:
        Dim inputFile As StreamReader
        Dim allData As String

        'with OpenFileDialog1, assign...
        With OpenFD1
            .Filter = "Text files (*.txt) | *.txt"
            .InitialDirectory = "C:\"
            .Title = "Select a File to Open..."

            'If user clicks Open, then...
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                inputFile = File.OpenText(.FileName)
                allData = inputFile.ReadToEnd
                MessageBox.Show(allData)
            End If

        End With


Is This A Good Question/Topic? 0
  • +

Replies To: Text File to Array, Line by Line

#2 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 03:24 AM

...tries for hours, posts question on forum, gets lucky and figures it out a minute later- weird right? I added a Do loop and implemented the use of .Peek:


            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                inputFile = File.OpenText(.FileName)

                Do Until inputFile.Peek = -1
                    allData = inputFile.ReadLine
                    MessageBox.Show(allData)
                Loop



BUT, still not clear on putting these line-by-line values into an array of 3 values. Anybody? TIA.
Was This Post Helpful? 0
  • +
  • -

#3 lucky3  Icon User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 231
  • View blog
  • Posts: 765
  • Joined: 19-October 11

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 08:55 AM

Use the .Split on each line you read.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 09:13 AM

View PostVisuallyBasic, on 08 December 2012 - 04:09 AM, said:

I have a txt file, example:
"12,34,56
78,91,23
...
You get the point- it contains sets of 3 digits followed by line breaks. I need to split the contents line by line and place into an array(2d) for usage further along. I don't know if it's my memory or what, but the pieces ain't fitting. I really cannot stand arrays sometimes. I've gotten the entire file contents to display, but all the loops I've tried have shown the digits 1-by-1 instead of line-by-line... any hints/ideas? Any advice much appreciated, guys.

Why a 2D array? If you are contrained by a school project, then I would ask why you would be asked to put three values into a 2D array.

There are many ways to store these values, including Arrays, Lists, and probably best of all, as members of a Class. The traditional way (from the 'olden days') :) would be to use a 3D array, one dimension for each value.

A more modern approach might be to use a 1D array, and put the array in a List of arrays.

A still more modern approach might be to create a Class to hold the three values, and a few Functions to manipulate them. Each New class member could be placed in an array or list.

Mull these over.
Was This Post Helpful? 0
  • +
  • -

#5 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 01:31 PM

View Postlar3ry, on 08 December 2012 - 09:13 AM, said:

View PostVisuallyBasic, on 08 December 2012 - 04:09 AM, said:

I have a txt file, example:
"12,34,56
78,91,23
...
You get the point- it contains sets of 3 digits followed by line breaks. I need to split the contents line by line and place into an array(2d) for usage further along. I don't know if it's my memory or what, but the pieces ain't fitting. I really cannot stand arrays sometimes. I've gotten the entire file contents to display, but all the loops I've tried have shown the digits 1-by-1 instead of line-by-line... any hints/ideas? Any advice much appreciated, guys.

Why a 2D array? If you are contrained by a school project, then I would ask why you would be asked to put three values into a 2D array.

There are many ways to store these values, including Arrays, Lists, and probably best of all, as members of a Class. The traditional way (from the 'olden days') :)/>/>/> would be to use a 3D array, one dimension for each value.

A more modern approach might be to use a 1D array, and put the array in a List of arrays.

A still more modern approach might be to create a Class to hold the three values, and a few Functions to manipulate them. Each New class member could be placed in an array or list.

Mull these over.


Got some rest, feeling a little better. 12 hours left on this one. Thanks for your response, maybe I'm not understanding this because I'm still not solid on arrays. I hate copying assignment notes word for word (for the sake of future students taking this course) but whatever:


Using the previous assignment as a seed for breaking the string into the three numbers write code that:
  • Reads the data from a data file, line by line.
  • Each line that is read must be passed to a function that returns an array of three numbers (do not use String arrays to break the string into three numbers).
  • Assume the delimiter is provided by the user (so it could be comma as shown below, or it could be Tab, or space, etc).
  • Once the data is read into an 2-D array, write a handler code that displays the final score which is 50% of the average of the first two grades + 50% of the third grade. For each row, display yhe final score and a letter grade of A, B, C, D or F.
  • The handler above should also show the min, max, avg and number of grades below average as in the previous two assignments. (this last one was bold)



Right. Also, we just finished up array's, next in our book is multidimensional arrays, then using files- assume I've already read through those chapters and next week is our final class. So naturally, I think our professors intent is for us to use multidimensional arrays? In any case! I'm just gonna post these notes for anyone who has any ideas, and think your ideas through, I may resort to an alternative like you mentioned. Also, is it just me or can we not edit posts on this forum? Hm.
Was This Post Helpful? 0
  • +
  • -

#6 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 01:47 PM

View Postlucky3, on 08 December 2012 - 08:55 AM, said:

Use the .Split on each line you read.


Assume the form has a button (open) and a textbox (delimiter)...

                Do Until inputFile.Peek = -1
                    allData = inputFile.ReadLine

                    array = Split(allData, delim)
                    For x As Integer = 0 To array.GetUpperBound(0)
                        MessageBox.Show(array(x))
                    Next


Played around with my code a bit, this would throw all values into a single array, right? But then how do I go about placing them into an array of values of 3?
Was This Post Helpful? 0
  • +
  • -

#7 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 09:27 PM

Public Function ReadFile() As String

        Dim inputFile As StreamReader
        Dim allData As String

        Dim delim As String = text1.Text

        Dim array() As String
        Dim oneByone As String
        Dim divide_maxrow As String

        Dim newArray(,) As Double
        Dim newArray2(,) As Double

        With OpenFD1
            .Filter = "Text files (*.txt) | *.txt"
            .InitialDirectory = "C:\"
            .Title = "Select a File to Open..."

            'If user clicks Open, then...
            If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                inputFile = File.OpenText(.FileName)
                allData = inputFile.ReadToEnd

                'MessageBox.Show(allData)
                array = Split(allData, delim)
                For x As Integer = 0 To array.GetUpperBound(0)
                    oneByone = (array(x))
                    divide_maxrow = CStr((x + 1) / 3)
                    MessageBox.Show(divide_maxrow)
                Next


                For intRow As Integer = 0 To CInt(divide_maxrow)
                    For intCol As Integer = 0 To 2
                        newArray2 = newArray(CInt((divide_maxrow)), 2)

                    Next
                Next


                'inputFile.Close()
            End If
        End With

        Return newarray

    End Function


I apologize for all the posts today, but I'm really rushed. Line 36 is giving me a "Value of type 'Double' cannot be converted to '2-dimensional array of Double'" I am calculating the sizes of my array so I can place the digits from my text file, into the array. Error is throwing me off though, anyone?
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Text File to Array, Line by Line

Posted 08 December 2012 - 09:29 PM

View PostVisuallyBasic, on 08 December 2012 - 02:47 PM, said:

View Postlucky3, on 08 December 2012 - 08:55 AM, said:

Use the .Split on each line you read.

Unfortunately, VisualBasic, if I am reading the assignment rules correctly, you are not allowed to use a string array to extract the values: "do not use String arrays to break the string into three numbers", so .Split is not to be used. As well, I believe that when the rules stated a (2D array), what it actually meant was an array Dim'd like this: Dim grades(2) as Integer, which is a 1-D array with 3 elements (0, 1 and 2).

What you will want to do is to break out the numbers with a different method, and the easiest I can think of is to use .IndexOf to look for delimiters, .Substring to extract the number strings one at a time, and CInt(numberstring) to convert and assign to an array of integers.

I know it probably sounds like a lot, but it really isn't.

In your main code, you'll need to Dim a StreamReader, an Integer array (2), and an inputline string variable.
Then use your StreamReader to read a line at a time, and then to call your function, assigning the returned value to the integer array. Call it with the String you read in and the delimiter.

In your function, Dim an integer array (2) and an index integer variable. Then, you can use a For/Next loop, stepping from 0 to 2 (because you always have 3 items to consider in one line from the text file). Inside the loop, use IndexOf, Substring and Cint() to place your values in the local (inside your Function) array, then Return that array.

Read all this over, and take it one step at a time, testing as you go, and get back to us with your success or further questions. Once you have this figured out, we can work on the rest of the problem.
Was This Post Helpful? 0
  • +
  • -

#9 VisuallyBasic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 16-November 12

Re: Text File to Array, Line by Line

Posted 09 December 2012 - 01:50 AM

View Postlar3ry, on 08 December 2012 - 09:29 PM, said:

View PostVisuallyBasic, on 08 December 2012 - 02:47 PM, said:

View Postlucky3, on 08 December 2012 - 08:55 AM, said:

Use the .Split on each line you read.

Unfortunately, VisualBasic, if I am reading the assignment rules correctly, you are not allowed to use a string array to extract the values: "do not use String arrays to break the string into three numbers", so .Split is not to be used. As well, I believe that when the rules stated a (2D array), what it actually meant was an array Dim'd like this: Dim grades(2) as Integer, which is a 1-D array with 3 elements (0, 1 and 2).

What you will want to do is to break out the numbers with a different method, and the easiest I can think of is to use .IndexOf to look for delimiters, .Substring to extract the number strings one at a time, and CInt(numberstring) to convert and assign to an array of integers.

I know it probably sounds like a lot, but it really isn't.

In your main code, you'll need to Dim a StreamReader, an Integer array (2), and an inputline string variable.
Then use your StreamReader to read a line at a time, and then to call your function, assigning the returned value to the integer array. Call it with the String you read in and the delimiter.

In your function, Dim an integer array (2) and an index integer variable. Then, you can use a For/Next loop, stepping from 0 to 2 (because you always have 3 items to consider in one line from the text file). Inside the loop, use IndexOf, Substring and Cint() to place your values in the local (inside your Function) array, then Return that array.

Read all this over, and take it one step at a time, testing as you go, and get back to us with your success or further questions. Once you have this figured out, we can work on the rest of the problem.


I am so dead right now, I don't think I've ever sat this long at a computer even from when I was a kid playing computer games all day/night. I tried a lot of different variations of code and there's really a point after staring at code for so long where your brain/upperback just can't take it any longer, I've experienced that breaking point for the past like 3 weekends- lol. I realized the assignment rules way too late in the process this time around. Sleep is probably the best thing for me right now. You've been a great help, thanks man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1