Click here to Skip to main content
15,887,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello. I was trying to read a text file which has content like this :

hello:hi,bye,cu,goodbye
later:late,very late, ver very late

etc. etc.

I want to read this document line by line, split each line on ":" char. Then store first value as a key in dictionary and second as value.

this is my code:

VB
Dim sr As StreamReader = New StreamReader(sFileName)
        sInputLine = sr.ReadLine()
        Do Until sInputLine Is Nothing
            sInputLine = sr.ReadLine()
            If (sInputLine <> "") Then
                Dim sites As String() = Nothing
                sites = sInputLine.Split(":")
                Dim a, b As New ArrayList
                a.Add(sites(0))
                b.Add(sites(1))
                If (sync.syndict.ContainsKey(a.ToString)) Then
                    sync.syndict.Item(a.ToString) = sync.syndict.Item(a.ToString) + "," + b.ToString
                Else
                    sync.syndict.Add(a.ToString, b.ToString)
                End If
            End If
        Loop
        sr.Close()



but its not working. Any suggestions?

Also when i use a tring array in place of arraylist like this

VB
Dim sites2(2) As String
sites2 = sr.ReadLine.Split(":")
            sync.syndict.Add(sites2(0), sites2(1))


it shows an error : object not initialized, on first line (split staetment)
Posted
Updated 28-Sep-10 22:17pm
v2

1 solution

Hi,
I checked the code.
It is working fine.

Here is a sample that I have re-written.
Dim sFileName As String = "C:\test.txt"
       Dim syndict As New Dictionary(Of String, String)
       Dim sr As StreamReader = New StreamReader(sFileName)
       Dim sInputLine As String = sr.ReadLine()
       Do Until sInputLine Is Nothing
           If (sInputLine <> "") Then
               Dim sites As String() = Nothing
               sites = sInputLine.Split(":")
               If (syndict.ContainsKey(sites(0))) Then
                   syndict.Item(sites(0)) = syndict.Item(sites(0)) + "," + sites(1)
               Else
                   syndict.Add(sites(0), sites(1))
               End If
           End If
           sInputLine = sr.ReadLine()
       Loop
       sr.Close()
   End Sub


Kindly let know if this is solving your issue.
Or else specify where exactly this object not initialized exception is shown.

If necessary you can add check for the splitted string array(sites) before accessing it by index or catch exception for that.
Also make sure that each line has only one ':'.Otherwise you may get unexpected results.
 
Share this answer
 
v2
Comments
amit_upadhyay 29-Sep-10 8:22am    
your code works fine. I didnt get what was wrong with my code? Anyways maybe your rewritten code has some advantages over mine. Thanks..
amit_upadhyay 29-Sep-10 8:23am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Rahul P Nath 30-Sep-10 0:03am    
I think you should try finding out why it was not working,so it helps you more.
Feel free to ask if you need any help on that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900