Reading from a text file and then splitting into 2 listboxs

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 809 Views - Last Post: 03 August 2012 - 05:37 AM Rate Topic: -----

#1 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:14 AM

Hi

I am currently having a problem with Reading from a text file and then splitting into 2 listboxs. I am able to read from the textfile no problems, but the contents is not splitting up into the right listboxs. It always goes into one or not at all. At the moment I am getting the error: Conversion from string "F" to type 'Double' is not valid.

What I am trying to do is get the contents of the textfile split into a male or female category. Using M for male and F for female in the textfile. If anyone could help me, that would be fantastic.

Imports System.IO

Public Class Form1
    Dim firstname(20), lastname(20), applicationdate(40), address(40), gender(40) As String
    Dim totalM, totalF, grandtotal, M, F, loops As Single
    Dim genderM(20), genderF(20) As Boolean
    Dim itemcount As Integer

    Private Sub btnRead_Click(sender As System.Object, e As System.EventArgs) Handles btnRead.Click
        dlgImport.ShowDialog()
        Dim waiting As StreamReader = File.OpenText(dlgImport.fileName)

        Do While waiting.Peek <> -1
            firstname(itemcount) = waiting.ReadLine()
            lastname(itemcount) = waiting.ReadLine()
            gender(itemcount) = waiting.ReadLine()
            applicationdate(itemcount) = waiting.ReadLine()
            address(itemcount) = waiting.ReadLine()
            If gender(loops) = M Then
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(genderM(itemcount))
                lstFDisplay.Items.Add(applicationdate(itemcount))
                lstFDisplay.Items.Add(address(itemcount))
            ElseIf gender(loops) = F Then
                lstFDisplay.Items.Add(firstname(itemcount))
                lstFDisplay.Items.Add(lastname(itemcount))
                lstFDisplay.Items.Add(genderF(itemcount))
                lstFDisplay.Items.Add(applicationdate(itemcount))
                lstFDisplay.Items.Add(address(itemcount))
                itemcount = itemcount - 1
            End If
        Loop
        waiting.Close()
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Reading from a text file and then splitting into 2 listboxs

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • Joined: 12-June 08

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:27 AM

What's the logic on how to go to one text box vs another? What's a sample of the data look like?
Was This Post Helpful? 1
  • +
  • -

#3 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:31 AM

This is a sample of the data from the text file the boss gave me, he wants to split up the customers into male and female.

Trudi
Watson
F
3/08/2011
"15 George Rd, Glen Waverly"
Joshua
Blackwell
M
8/10/2011
"19 Lance Court, Oakleigh"
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • Joined: 12-June 08

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:34 AM

I am guessing the records break up like this?

Trudi
Watson
F
3/08/2011
"15 George Rd, Glen Waverly"

-------

Joshua
Blackwell
M
8/10/2011
"19 Lance Court, Oakleigh"

If that's the case then it is straight forward:

Read lines, store into variables, and count to five (because there's five lines).
When you hit five figure out what the sex is.
Send data accordingly.
Clear out variables and reset counter to 0.
Repeat until the file is done.
Was This Post Helpful? 1
  • +
  • -

#5 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:39 AM

Before I starting changing the code, it came out like this
Trudi
Watson
F
3/08/2011
"15 George Rd, Glen Waverly"
Joshua
Blackwell
M
8/10/2011
"19 Lance Court, Oakleigh"

There was no spaces, but now I get the error I mentioned above cause I couldn't seem to get it to read the sex of the person so they always went into the male list.

View Postmodi123_1, on 02 August 2012 - 08:34 AM, said:

I am guessing the records break up like this?

Trudi
Watson
F
3/08/2011
"15 George Rd, Glen Waverly"

-------

Joshua
Blackwell
M
8/10/2011
"19 Lance Court, Oakleigh"

If that's the case then it is straight forward:

Read lines, store into variables, and count to five (because there's five lines).
When you hit five figure out what the sex is.
Send data accordingly.
Clear out variables and reset counter to 0.
Repeat until the file is done.

Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • Joined: 12-June 08

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:44 AM

The line being read in is a string, right? M is actually "M"... F is "F".. the quotes are important for comparison and to signify it's a string. Slap a break point on the line that error is happening and examine what you are trying to shove the string into. I think you'll be amused.
Was This Post Helpful? 1
  • +
  • -

#7 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:54 AM

I fixed up some obvious errors now and now I don't get any errors anymore, but now no text shows up. I know the error is under my nose but I can't see it. It must be cause its 2am.

View Postmodi123_1, on 02 August 2012 - 08:44 AM, said:

The line being read in is a string, right? M is actually "M"... F is "F".. the quotes are important for comparison and to signify it's a string. Slap a break point on the line that error is happening and examine what you are trying to shove the string into. I think you'll be amused.

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • Joined: 12-June 08

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 08:57 AM

Again.. your code isn't that complex... debug. Put a break point at the top of the code and step through each line... you are guessing something is a type that it isn't.. .. that and I have no idea what your code looks like now.
Was This Post Helpful? 1
  • +
  • -

#9 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 09:01 AM

Thanks for your help, I will do that in the morning when my brain is switched on. Hopefully then I will get it.

View Postmodi123_1, on 02 August 2012 - 08:57 AM, said:

Again.. your code isn't that complex... debug. Put a break point at the top of the code and step through each line... you are guessing something is a type that it isn't.. .. that and I have no idea what your code looks like now.

Was This Post Helpful? 0
  • +
  • -

#10 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 09:13 PM

I did the breakpoint on every line and I keep seeing it loop back to the first read line, but I can't work out what is the mistake, I am a novice at this. What am I guessing something is a type of that it isn't. Sorry I am just very confused at this moment.
Was This Post Helpful? 0
  • +
  • -

#11 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 10:21 PM

Look at this line, I dont get the logic here!
If gender(loops) = M Then

Why are you checking for loop item? Isnt that required to be itemcount.
Also please post your updated code so we can go together.
Was This Post Helpful? 1
  • +
  • -

#12 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 02 August 2012 - 10:33 PM

I don't get my logic on that either, I had been using itemcount, but it wasn't working so I switched it to loops hoping it would work. When something doesn't work for me, I just start switching code around and I think that is creates more problems. Thanks for helping me.

Imports System.IO

Public Class Form1
    Dim firstname(20), lastname(20), applicationdate(40), address(40), gender(40), M, F As String
    Dim totalM, totalF, grandtotal, loops As Single
    Dim genderM(20), genderF(20) As Boolean
    Dim itemcount As Integer

    Private Sub btnRead_Click(sender As System.Object, e As System.EventArgs) Handles btnRead.Click
        dlgImport.ShowDialog()
        Dim waiting As StreamReader = File.OpenText(dlgImport.fileName)

        Do While waiting.Peek <> -1
            firstname(itemcount) = waiting.ReadLine()
            lastname(itemcount) = waiting.ReadLine()
            gender(itemcount) = waiting.ReadLine()
            applicationdate(itemcount) = waiting.ReadLine()
            address(itemcount) = waiting.ReadLine()
            If gender(itemcount) = M Then
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(genderM(itemcount))
                lstMDisplay.Items.Add(applicationdate(itemcount))
                lstMDisplay.Items.Add(address(itemcount))
                itemcount = itemcount - 1
            ElseIf gender(itemcount) = F Then
                lstFDisplay.Items.Add(firstname(itemcount))
                lstFDisplay.Items.Add(lastname(itemcount))
                lstFDisplay.Items.Add(genderF(itemcount))
                lstFDisplay.Items.Add(applicationdate(itemcount))
                lstFDisplay.Items.Add(address(itemcount))
                itemcount = itemcount - 1
            End If
        Loop
        waiting.Close()
    End Sub
End Class



View Postsmohd, on 02 August 2012 - 10:21 PM, said:

Look at this line, I dont get the logic here!
If gender(loops) = M Then

Why are you checking for loop item? Isnt that required to be itemcount.
Also please post your updated code so we can go together.

Was This Post Helpful? 0
  • +
  • -

#13 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading from a text file and then splitting into 2 listboxs

Posted 03 August 2012 - 12:26 AM

Also what do you intend to get here?
itemcount = itemcount - 1

I expect you will get errors because itemcount wa first 0 and you are subtracting 1 in every iteration. You have to add there not sub...
Again here:
If gender(itemcount) = M Then

M was not initialized and so it wont match your string, you have to do:
If gender(itemcount) = "M" Then
look I put quotation there. The same case to the else part, make it "F"

Another case here:
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(genderM(itemcount))
                lstMDisplay.Items.Add(applicationdate(itemcount))
                lstMDisplay.Items.Add(address(itemcount))

What exactly do you want to add in lstMDisplay??? I see things like genderM which has not been given value anywhere!
Edit those things and less us know what you get
Was This Post Helpful? 1
  • +
  • -

#14 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Reading from a text file and then splitting into 2 listboxs

Posted 03 August 2012 - 01:28 AM

I have made some changes and taken out the genderM and genderF, I was using them first and then decided to just use M and F instead. I still get the same result of nothing showing up in either list.

What I want to show up is firstname, lastname, gender, applicationdate and address.

Imports System.IO

Public Class Form1
    Dim firstname(20), lastname(20), applicationdate(40), address(40), gender(40), M, F As String
    Dim totalM, totalF, grandtotal, loops As Single
    Dim itemcount As Integer

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        dlgImport.ShowDialog()
        Dim waiting As StreamReader = File.OpenText(dlgImport.fileName)

        Do While waiting.Peek <> -1
            firstname(itemcount) = waiting.ReadLine()
            lastname(itemcount) = waiting.ReadLine()
            gender(itemcount) = waiting.ReadLine()
            applicationdate(itemcount) = waiting.ReadLine()
            address(itemcount) = waiting.ReadLine()
            If gender(itemcount) = "M" Then
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(gender(itemcount))
                lstMDisplay.Items.Add(applicationdate(itemcount))
                lstMDisplay.Items.Add(address(itemcount))
                itemcount = itemcount + 1
            ElseIf gender(itemcount) = "F" Then
                lstFDisplay.Items.Add(firstname(itemcount))
                lstFDisplay.Items.Add(lastname(itemcount))
                lstFDisplay.Items.Add(gender(itemcount))
                lstFDisplay.Items.Add(applicationdate(itemcount))
                lstFDisplay.Items.Add(address(itemcount))
                itemcount = itemcount + 1
            End If
        Loop
        waiting.Close()
    End Sub
End Class



View Postsmohd, on 03 August 2012 - 12:26 AM, said:

Also what do you intend to get here?
itemcount = itemcount - 1

I expect you will get errors because itemcount wa first 0 and you are subtracting 1 in every iteration. You have to add there not sub...
Again here:
If gender(itemcount) = M Then

M was not initialized and so it wont match your string, you have to do:
If gender(itemcount) = "M" Then
look I put quotation there. The same case to the else part, make it "F"

Another case here:
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(genderM(itemcount))
                lstMDisplay.Items.Add(applicationdate(itemcount))
                lstMDisplay.Items.Add(address(itemcount))

What exactly do you want to add in lstMDisplay??? I see things like genderM which has not been given value anywhere!
Edit those things and less us know what you get

Was This Post Helpful? 0
  • +
  • -

#15 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Reading from a text file and then splitting into 2 listboxs

Posted 03 August 2012 - 03:41 AM

Are you sure that you file has no empty lines at the beginning? Because the conditions will be false. Try to debug to see what data has been assigned to those variables in each iteration
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2