Results 1 to 20 of 20

Thread: Reading text file - passing over info [Resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11

    Reading text file - passing over info [Resolved]

    Ok, am a complete newbie with visual basic,
    My problem is that I can't work out how to not include data in a listbox that is not specific to what I want to show.

    I have a DoLoop that reads through the information in a textfile and I only want to show certain parts of the file, not the whole file.

    This is the code I have so far:



    VB Code:
    1. Private Sub btnClick1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick1.Click
    2.     Dim sr1 As IO.StreamReader = IO.File.OpenText("C:students.txt")
    3.     Dim dblage As Double
    4.     Dim strnum, strname, strmarstat, strgender As String
    5.     Dim fmtstr As String = "{0,-15}  {1,-15} {2,5} {3,5}    {4,5}"
    6.     With (lstReport.Items)
    7.       .Clear()
    8.       .Add("                          Student Report")
    9.       .Add("Student" & "               " & "Name" & "         " & "Age" & "   " & "Marital" & "  " & "Gender")
    10.       .Add("  No.  " & "               " & "    " & "         " & "   " & "   " & "Status " & "  " & "      ")
    11.     End With
    12.  
    13.     Checkgender(strgender)
    14.     Do While sr1.Peek <> -1
    15.       strnum = sr1.ReadLine
    16.       strname = sr1.ReadLine
    17.       strmarstat = sr1.ReadLine
    18.       dblage = CDbl(sr1.ReadLine)
    19.       strgender = sr1.ReadLine
    20.     Loop
    21.     sr1.Close()
    22.   End Sub
    23.   [color = red]Sub Checkstudentnum(ByRef strnum As String)
    24.     Dim dblage As Double
    25.     Dim strname, strmarstat, strgender, fmtstr As String
    26.     If Not strnum <> 0 Then
    27.       MsgBox("Invalid Student Number")
    28.       If strnum <> 0 Then
    29.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    30.       End If
    31.     End If
    32.   End Sub
    33.   Sub Checkgender(ByRef strgender As String)
    34.     Dim dblage As Double
    35.     Dim strnum, strname, strmarstat As String
    36.     Dim fmtstr As String = "{0,-15}  {1,-15} {2,5} {3,5}    {4,5}"
    37.     If strgender = "M" Then
    38.       If strgender = "F" Then
    39.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    40.       End If
    41.     End If
    42.  
    43.   End Sub[/color]
    44.   Private Sub btnClick2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClick2.Click
    45.     Dim sr As IO.StreamReader = IO.File.OpenText("C:students.txt")
    46.     Dim dblage As Double
    47.     Dim strnum, strname, strmarstat, strgender As String
    48.     Dim fmtstr As String = "{0,-15}  {1,5}"
    49.     With (lstReport.Items)
    50.       .Clear()
    51.       .Add("     Eligible Report")
    52.       .Add("Name" & "               " & "Age")
    53.     End With
    54.     Do While sr.Peek <> -1
    55.       If dblage > 30 Then
    56.         lstReport.Items.Add(String.Format(fmtstr, strname, dblage))
    57.       End If
    58.       strnum = sr.ReadLine
    59.       strname = sr.ReadLine
    60.       strmarstat = sr.ReadLine
    61.       dblage = CDbl(sr.ReadLine)
    62.       strgender = sr.ReadLine
    63.  
    64.     Loop
    65.     sr.Close()
    66.  
    67.   End Sub


    What am I missing? Even if anyone can steer me in the right direction I'd be grateful.

    Thanks in advance,
    Jazdrew
    Last edited by jazdrew; Sep 6th, 2004 at 02:08 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    You're missing the part where you're supposed to tell us what you don't want in the listbox, and which portion of the code we're supposed to be looking at.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    Forgot to put in what I am wanting to show.

    For one section I only want to show Single names over 30. I have the coding to show the over 30's but can't work out how to dilute this down more so it only shows single people.

    The other section I want to show all names that have either M(Male) or F(Female) as the gender but it shows all names regardless of what letter is in the gender line.
    Also to only show names that have all information correct, ie. to leave out any names that don't have the correct information.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    VB Code:
    1. If strgender = "M" Then
    2.       If strgender = "F" Then
    3.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    4.       End If


    this is the code that I have tried for only selecting M or F gender but it still brings up all gender inputs.


    VB Code:
    1. Do While sr.Peek <> -1
    2.       If dblage > 30 Then
    3.         lstReport.Items.Add(String.Format(fmtstr, strname, dblage))
    4.       End If
    5.       strnum = sr.ReadLine
    6.       strname = sr.ReadLine
    7.       strmarstat = sr.ReadLine
    8.       dblage = CDbl(sr.ReadLine)
    9.       strgender = sr.ReadLine
    10.  
    11.     Loop
    12.     sr.Close()

    this is the code that i have for selecting over 30's but can't work out how to also select those that are single. this one shows all over 30's regardless of marital status.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174

    Re: Reading text file - passing over info

    For both the conditions, you can do this:

    VB Code:
    1. If dblage > 30 AndAlso strmarstate = "Widow" Then

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    I just noticed something in your code...


    What exactly are you doing here:


    VB Code:
    1. If Not strnum <> 0 Then
    2.       MsgBox("Invalid Student Number")
    3.       If strnum <> 0 Then
    4.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    5.       End If
    6.     End If

    ?


    You have a nested If statement, both checking the same variable, strnum. In this case, if strnum IS EQUAL to zero, then it'll skip right over the block, and never get to the part where you compare "If strnum <> 0" You should bring that if block outside. Like this:

    VB Code:
    1. If Not strnum <> 0 Then
    2.       MsgBox("Invalid Student Number")
    3.    Else If strnum <> 0 Then
    4.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    5.       End If


    You also have a similar thing here:

    VB Code:
    1. If strgender = "M" Then
    2.       If strgender = "F" Then
    3.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    4.       End If
    5.     End If

    Your turn, what should it be replaced with?

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    Ok. Got the first part worked out with having over 30 and Singles only showing. Had tried using only AND and couldn't get it to work so was stumped. Have never seen anything with ANDALSO.



    Still not sure on how to do the part with showing only M or F gender. Tried the ElseIf with it but it only brings up F gender not both.

    The strnum code is to try and only show those numbers that are only numbers, not numbers with a letter in them.

    eg.

    050656565 - show this one
    056565T65 - don't show this one

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    OK, what do you want to accomplish here:

    VB Code:
    1. If strgender = "M" Then
    2.       If strgender = "F" Then
    3.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    4.       End If
    5.     End If

    ?

    Only male? Only female? Only male or female? Both? Neither?

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    Both genders. I want to show all M and F and leave out any other letters that may be in that category.

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    VB Code:
    1. If strgender = "M" Or strgender = "F" Then
    2.  
    3.         lstReport.Items.Add(String.Format(fmtstr, strnum, strname, dblage, strgender, strmarstat))
    4.  
    5.     End If

  11. #11
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Originally posted by jazdrew
    Had tried using only AND and couldn't get it to work so was stumped.
    This shouldn't be happening. Once this thread is resolved, go back and try putting in And and then step through.

  12. #12

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11

    Smile

    That's fixed that problem

    had been trying:

    If strgender = "M" or "F" Then

    and kept getting an error message about "F" and couldn't for the life of me work out why.


    Any suggestions on the strnum problem?
    Would be so much easier if you could just type in exactly what you wanted to do

    eg. Don't show any number that has a letter in it

  13. #13
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Try

    If IsNumeric(strnum) Then
    ...

  14. #14

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    excellent.


    though how do you then stop the invalid number showing up on the screen with all other data after you press the message box button?

    am sorry for asking all these things but the book i have that came with this program must only show you all the really basic stuff. will have to go and buy a book that actually teaches you stuff.

  15. #15
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Do you actually want to keep showing the MessageBox for every invalid number that comes up in the list, or do you simply want to ignore it?

  16. #16

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    I think ignore it.

    After seeing it work I think it is really pointless other than to let you know that there is information that needs fixing.

    As long as the program ignores the invalid data and only shows the correct data.



    One last question. Sorry.

    how do you total up how many the same in type?
    that is, how many single people or how many females?
    What is the coding for that?

  17. #17
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Originally posted by jazdrew
    I think ignore it.

    After seeing it work I think it is really pointless other than to let you know that there is information that needs fixing.

    As long as the program ignores the invalid data and only shows the correct data.

    Then simply remove the MessageBox.Show line.

    That condition needn't be checked, in fact. Just check If IsNumeric() and add what you need to. Don't look at the other condition. It's unnecessary.





    One last question. Sorry.

    how do you total up how many the same in type?
    that is, how many single people or how many females?
    What is the coding for that?
    Declare an integer variable, and in your If statements, everytime you either come across a person of maritStatus = "single", increment the value by one. Same applies for the female thing.

    I will not show you this just yet, or you'll become entirely dependent and not try it out yourself. I think you should try out what I've suggested first and then come back when you're stuck. Show me what you've tried.

  18. #18

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    Thanks.
    Will work on this prob tonight and post back tomorrow with outcome.

  19. #19

    Thread Starter
    New Member
    Join Date
    Aug 2004
    Location
    Australia
    Posts
    11
    VB Code:
    1. If strmarstat = "M" AndAlso strgender = "M" Then
    2.         intTotalMM += 1
    3.       End If
    4.       If strmarstat = "M" AndAlso strgender = "F" Then
    5.         intTotalMF += 1
    6.       End If
    7.       If strmarstat = "S" AndAlso strgender = "M" Then
    8.         intTotalSM += 1
    9.       End If
    10.       If strmarstat = "S" AndAlso strgender = "F" Then
    11.         intTotalSF += 1
    12.       End If



    not sure yet how to get this to show on the report though under the names that show at the moment with the original code.

  20. #20
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,174
    Uhm, sorry. Is that question or a statement? If it's a question, what's the question?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width