Results 1 to 4 of 4

Thread: [RESOLVED] Putting comma separated lines of text into array structures

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    7

    Resolved [RESOLVED] Putting comma separated lines of text into array structures

    I am pretty clueless when it comes to Visual Basic. Im not too bad with basic things, but I'm having difficulty figuring this one out. For my homework, I have a text file which contains lines of values which correspond to complex numbers. The file looks like this:

    2,3
    3,4
    4,4
    5,5
    5,4
    etc.

    Where the first number is the real part and the second number is the complex part. I need to put the first parts of all of the lines into one array and the second part into a second array and somehow incorporate an array structure. The hint my professor gave me was "You need to use the array concept to read information from file. Using Structure and then creating an array of Structure would be wise." He gave us a couple lines of code as an example.

    Code:
    Dim Real_Part() as Integer
    Dim imaginary_part() As Integer
    
    Structure ComplexNum
          Dim real As Integer
          Dim imaginary as integer
    EndStructure
    
    'array of complex numbers
    
    Dim complexNums() As ComplexNum
    Dim formatted_output As String
    With what we've learned up to this point, we would use loops to read each line using an IO.StreamReader. This is my code so far.

    Code:
        Private Sub btnFileAdd_Click(sender As Object, e As EventArgs) Handles btnFileAdd.Click
            InputFile = IO.File.OpenText("C:\Users\Joe\Documents\Visual Studio 2012\Projects\Lab 7\LAB 7 Download Files\input.txt")
            Dim idx As Integer
            idx = 0
    
            Do Until InputFile.EndOfStream
                ReDim Preserve RealPart(idx)
                RealPart(idx) = InputFile.ReadLine().Split(",")
                MessageBox.Show(RealPart(idx))
                idx += 1
            Loop
        End Sub
    End Class
    Im getting an error message that says "Value of '1-dimensional type array' cannot be converted to 'Integer'"

    Honestly, I have no idea if this is the slightest bit right and Im totally stuck at this point. Any help would be appreciated.

    -Joe

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: Putting comma separated lines of text into array structures

    Hi and Welcome to the Forum.

    There are easier ways to do this sort of thing in VB.NET but since this is an assignment lets stick with what you have got. Your issue is to do with the following line of code:-

    vb.net Code:
    1. Real_Part(idx) = InputFile.ReadLine().Split(","c)

    What you are currently saying here is:-

    Set a Specific Index of the Real_Part() Array to be Equal to the MULTIPLE Indexes of the InputFile.ReadLine().Split(","c) Array. You Cannot Say That! You must say that a Specific Index of a Destination Array is equal to another Specific Index of a Source Array.

    So, knowing that you need to read the Input File into a Temporary array and then set the Real_Part and Imaginary_Part arrays to the specific Indexes of the Temporary Array. Here is a quick example:-

    vb.net Code:
    1. Do Until InputFile.EndOfStream
    2.   ReDim Preserve Real_Part(idx)
    3.   ReDim Preserve Imaginary_Part(idx)
    4.  
    5.   'Create an Input Array
    6.   Dim inputArray As String() = InputFile.ReadLine().Split(","c)
    7.  
    8.   'Now Set Your Destination Arrays as you need to
    9.   Real_Part(idx) = inputArray(0)
    10.   Imaginary_Part(idx) = inputArray(1)
    11.  
    12.   idx += 1
    13. Loop

    I will now leave it to you to sort out the use of your Structure.

    Hope that helps and Good Luck.

    Cheers,

    Ian

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    Re: Putting comma separated lines of text into array structures

    Quote Originally Posted by jditrapani View Post
    Code:
                RealPart(idx) = InputFile.ReadLine().Split(",")
    Im getting an error message that says "Value of '1-dimensional type array' cannot be converted to 'Integer'"
    Well RealPart() is an Integer, ReadLine is returning a String, so you just need to convert the string parts to Integer, something like...

    Code:
        Structure ComplexNum
            Dim real As Integer
            Dim imaginary As Integer
        End Structure
    
        Dim complexNums() As ComplexNum
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    
            Dim InputFile = IO.File.OpenText("input.txt")
            Dim idx As Integer = 0
    
            Do Until InputFile.EndOfStream
                ' read line, split line by comma
                Dim lineParts() As String = InputFile.ReadLine.Split(","c)
                ' resize array w/preserve
                ReDim Preserve complexNums(idx)
                ' convert element to Integer, add to array
                complexNums(idx).real = CInt(lineParts(0))
                complexNums(idx).imaginary = CInt(lineParts(1))
                idx += 1
            Loop
            InputFile.Close()
    
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            ' show results in immediate window
            For Each item In complexNums
                Debug.WriteLine("real={0}, imaginary={1}", item.real, item.imaginary)
            Next
        End Sub
    Last edited by Edgemeal; Mar 29th, 2015 at 08:58 AM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2015
    Posts
    7

    Re: Putting comma separated lines of text into array structures

    Thanks for all the help, everyone. What I ended up doing, and I got 100% on the assignment, was this:
    Code:
            Do Until InputFile.EndOfStream
                ReDim Preserve store(x)
                store(x) = InputFile.ReadLine
                x += 1
            Loop
    
            For x = 0 To 15 Step 2
                a = store(x).Substring(0, store(x).IndexOf(","))
                b = store(x).Substring((store(x).IndexOf((",")) + 1))
                c = store(x + 1).Substring(0, store(x + 1).IndexOf(","))
                d = store(x + 1).Substring((store(x + 1).IndexOf(",")) + 1)
                lstAnswers.Items.Add(ComplexMath.MathFunctions.Add(a, b, c, d))
            Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width