Click here to Skip to main content
15,066,156 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello:
I am sorting an array and the first item on the list in an empty element. I made sure my text file does not have any empty lines but still I get the first element to be "Nothing"

Text file:
C
D
E
A
A
B

What I have tried:

VB
Dim File1Reader As New System.IO.StreamReader(TextBox1.Text)
Dim lineCount1 = File.ReadAllLines(TextBox1.Text).Length

Dim File1line As String
Dim file1items(lineCount1) As String
Do While File1Reader.Peek() <> -1
    File1line = File1Reader.ReadLine
    If File1line.Length > 0 Then
        file1items(i) = File1line
    End If

    i = i + 1
Loop
Dim filetable1 As New List(Of Data)

For x = 0 To (lineCount1 - 2)

    System.Array.Sort(Of String)(file1items)

    If file1items(x) = file1items(x + 1) Then
        c = c + 1
    Else
        c = 1
    End If

    filetable1.Add(New Data(file1items(x), c))
Next
Posted
Updated 8-May-16 15:01pm
v2
Comments
   
Empty string and Nothing are totally different things, to start with.
If you already performed ReadAllLine, why would you also need StreamReader.

Just these two points are absurd enough to stop at this point. Take it into account and write new code, start over. Then, if something is still not clear, come back. But need sooner then you used the debugger.

—SA
PIEBALDconsult 8-May-16 19:54pm
   
That, and calling ReadAllLines just to get the count. WTE!
   
Exactly.
—SA
Member 12471735 8-May-16 21:29pm
   
am using streamreader to read line by line and read all line to find total number of lines in the text
I understand length function is not a part of stream reader
I couldn't understand what is wrong with it

1 solution

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Possible problem: if you have a CR/LF after the last useful line, you have an empty line. Be attentive to the variables as you run the debugger. Said otherwise, it is data dependent, we can't test it for you.

Advice: Pay attention to the comments about your bad practices.
   
v4

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