ashraj98
ashraj98

Reputation: 384

Split string from file

I have a file in which the name of a book and its author is on each line. (EX: "Douglas Adams,The Hitchhiker's Guide To The Galaxy" is one line of the file). I can read each line into a temporary string, but when I split it at the comma to put the author and book in different arrays, it won't work.

Here is my code:

objReader = New StreamReader(AppPath() + "books\books.txt")
i = 1
Dim temp() As String
Dim tempStr As String
Do While objReader.Peek() <> -1
  tempStr = objReader.ReadLine()
  temp = tempStr.Split(New Char() {","c})
  temp(0) = authors(i)
  temp(1) = books(i)
  i = i + 1
Loop

I already initialized objReader and i earlier, and I imported System.IO, too. I have tried to change the delimiters to semicolons, slashes, and backslashes in both the code and the file, but it doesn't work. I can confirm the file loads correctly.

Upvotes: 0

Views: 94

Answers (1)

nmat
nmat

Reputation: 7591

You have to put the string in the arrays, you're doing it the other way around:

authors(i) = temp(0)
books(i) = temp(1)

Upvotes: 4

Related Questions