Results 1 to 6 of 6

Thread: *Answered*Need help printing 2d arrays to a text file

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    8

    *Answered*Need help printing 2d arrays to a text file

    Hello,

    I am a student currently taking an Adv. Visual Basics course. I am working on our first assignment and I got stuck on the part where I have to save the data to a file.

    We have to save 6 student names and 5 of their test scores as well as an average of the test scores. Currently the way I have it set up is with 3 arrays. One array holds the students names, one holds their test scores, which is a 2d array, and another holds their averages.

    The way I want the data to be saved to the file is..

    Student1 Name
    Test Score1
    TestScore2
    TestScore3
    Testscore4
    Testscore5
    Average: TestScoresAverage
    Student2 Name
    ....

    I am not sure how to set up my loops to get the data to save to this format, and I am also not sure how to set up a loop for the 2d array.

    Here is a picture of the program as well as the code which you might find helpful. Just note I still have other stuff to finish besides the saving the data part.

    Name:  632ffcec90.jpg
Views: 480
Size:  25.1 KB

    Code: http://pastebin.com/EjjmPXVp
    The function I use to save the data is at line 117. The current for loops I have don't work. I get an outofrange exception.
    Last edited by darthirule; Feb 12th, 2015 at 04:56 PM. Reason: no longer need help

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,347

    Re: Need help printing 2d arrays to a text file

    Without looking @ your code ... to save an array to a file:

    vb Code:
    1. My.Computer.FileSystem.WriteAllText(filename, Join(MyArray, vbCrLf), False)

    ... but since your array seems to contains objects you would probably do something like:

    vb Code:
    1. My.Computer.FileSystem.WriteAllText(filename, Join(MyArray.Select(Function(x) x.Name & vbTab & x.Score1 & vbTab & x.Score2 & vbTab & x.Score3 & vbTab & x.Score4 & vbTab & x.Score5).ToArray, vbCrLf), False)

    Kris

  3. #3
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: Need help printing 2d arrays to a text file

    If this is an exercise in loops...

    To loop through an array of strings you can do;
    VB Code:
    1. For Each Name As String In StudentNames
    2.     'Do something with Name
    3. Next

    To loop through a two dimensional array you can do;
    VB Code:
    1. For First As Integer = TestScores.GetLowerBound(0) To TestScores.GetUpperBound(0)
    2.     For Second As Integer = TestScores.GetLowerBound(1) To TestScores.GetUpperBound(1)
    3.         'Do something with TestScores(First, Second)
    4.     Next
    5. Next

    Similarly to an array of strings, to loop through an array of integers (or whatever type it is) you can do;
    VB Code:
    1. For Each AverageScore As Integer In Averages
    2.     'Do something with AverageScore
    3. Next

    To write to a file, you can do;

    VB Code:
    1. Dim w As new System.IO.StreamWriter("FileName.txt",False)
    2. w.WriteLine("A line")
    3. w.WriteLine("Another line")
    4. w.Close()


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    8

    Re: Need help printing 2d arrays to a text file

    Quote Originally Posted by Bulldog View Post
    If this is an exercise in loops...

    To loop through an array of strings you can do;
    VB Code:
    1. For Each Name As String In StudentNames
    2.     'Do something with Name
    3. Next

    To loop through a two dimensional array you can do;
    VB Code:
    1. For First As Integer = TestScores.GetLowerBound(0) To TestScores.GetUpperBound(0)
    2.     For Second As Integer = TestScores.GetLowerBound(1) To TestScores.GetUpperBound(1)
    3.         'Do something with TestScores(First, Second)
    4.     Next
    5. Next

    Similarly to an array of strings, to loop through an array of integers (or whatever type it is) you can do;
    VB Code:
    1. For Each AverageScore As Integer In Averages
    2.     'Do something with AverageScore
    3. Next

    To write to a file, you can do;

    VB Code:
    1. Dim w As new System.IO.StreamWriter("FileName.txt",False)
    2. w.WriteLine("A line")
    3. w.WriteLine("Another line")
    4. w.Close()

    The assignment is for Arrays and writing to a file.

    I want to use a loop to do all my file writing for me. I think it would look better and over all just a better style to go for instead of just writing a line of code for every line of text in the text document.

    We have to use arrays but loops are not required. Should I just not bother with a loop?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,398

    Re: Need help printing 2d arrays to a text file

    Try this:

    Code:
    Using sw As new System.IO.StreamWriter("FileName.txt",False)
        For y as Integer = 0 to studentNamesArray.GetUpperBound(0)
            sw.WriteLine(studentNamesArray(y)
            For x as integer = 0 to 4
                sw.WriteLine(testScoresArray(y,x))
            Next
            sw.WriteLine("Average: " & averagesArray(y))
        Next
    End Using
    Last edited by .paul.; Feb 12th, 2015 at 03:08 PM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2014
    Posts
    8

    Re: Need help printing 2d arrays to a text file

    I ended up just writing the file without loops.

    But thanks for all the suggestions! I definitely learned some new things.

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