0

Thank you for taking the time to read this question. My question is about VB.NET and converting a text file to a 1D array.

My text file will contain 30 names of technology companies. I want the data values read from TECHNOLOGY into a 1D array called 'companies'. I then want all the companies output with each on a new line.

Please let me know how I can improve my code / correct it fully. Thanks!

Dim sr As New Streamreader("Companies")
Dim words as string=""
Dim company(30) as string
Dim I as Integer=0

Do Until sr.Peek=1
Word=sr.Readline()
company(i)=word

Lstwords.Items.Add(words(i))

i=i+1

link to code as an Image

Doliveras
  • 1,794
  • 2
  • 14
  • 30
Alex
  • 1
  • 1
  • 2
  • 1
    Please put the code right into the question. – Luboš Turek May 03 '16 at 10:50
  • http://stackoverflow.com/questions/8037070/whats-the-fastest-way-to-read-a-text-file-line-by-line Read this for more ideas. Although, I don't think you will see much difference in a file this small. – jitendragarg May 03 '16 at 11:04

3 Answers3

1

You can accomplish this by using File.ReadAllLines

'Load each line of the file into an element of a string array
Dim companies() as string = System.IO.File.ReadAllLines("Companies")

For each company as string in companies
   Console.WriteLine(company)
next
Doliveras
  • 1,794
  • 2
  • 14
  • 30
0

I'm not sure what you mean at the end by "output each on a new line", so I've just done it in the console.

    Dim companies(29) As String
    Dim i As Integer = 0
    Dim sr As IO.StreamReader
    Dim filename As String
    filename = System.IO.Path.Combine(
    My.Computer.FileSystem.SpecialDirectories.MyDocuments, "companies.txt")

    If IO.File.Exists(filename) Then
        sr = IO.File.OpenText(filename)
        Do While sr.Peek <> -1
            companies(i) = sr.ReadLine
            i += 1
        Loop
        sr.Close()
    Else
        Console.WriteLine("File not found")
    End If

    For Each company As String In companies
        Console.WriteLine(company)
    Next

Be aware that this code won't work if you don't have exactly 30 lines of companies in the text file. Modify the code above to deal with contingencies.

p3tch
  • 1,414
  • 13
  • 29
  • Thanks @p3tch however I'm a bit confused. Why is it Dim companies(29) as string, not dim companies(30) as string? – Alex May 04 '16 at 09:15
  • Hi Alex, it's because arrays are zero-based. So `myArray(5)` would actually be the 6th item in the array. Similarly if you want to declare an array of 6 values, you would do so like `Dim values(5) As Integer` – p3tch May 04 '16 at 14:12
  • Also, have you considered using a List instead of an Array? You don't need to specify the length when declaring a list. – p3tch May 04 '16 at 14:21
0

If you want to improve this code :

Dim sr As New Streamreader("Companies")
Dim word As String = "" 'To remember the current word
Dim company(30) as string 'The list of companies
Dim i as Integer = 0 'a counter

Word = sr.Readline() 'We read the first line

While Not IsNothing(Word) 'While we read a line (See EDIT for explanation)
    Company(i) = word 'We add the word
    Lstwords.Items.Add(word) 'I suppose this is a list that you want to display
    i += 1 'Increment the counter
    Word = sr.ReadLine() 'Read the next line
End While 'End of the loop

EDIT : Why use Not IsNothing(Word)

If you look at the ReadLine() documentation, you find out that when the end of file is reached, that function will return Nothing.

So this is a necessary and sufficient way to know we are not at the end of the file.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • thanks! could you please explain the line: Not IsNothing(Word) i.e, why is that needed? Can it function without? Thanks! – Alex May 04 '16 at 09:17