Results 1 to 8 of 8

Thread: [RESOLVED] HELP with strings and fornext loop please!!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Resolved [RESOLVED] HELP with strings and fornext loop please!!

    Hi! I am trying to do this project for my Comp Prog class. The Project assignment is "Create an application that prompts the user to enter his/her and last name in a single text box. The program returns the user initials in uppercase".

    This is what i have. I am using listboxes for now instead to display the initial.

    Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Name, F, Last As String
    Dim Cnt As Integer = 0
    Name = TextBox1.Text
    Last = Name.Chars(cnt)
    For Cnt = 0 To (Name.Length) - 1
    Last = Name.Chars(Cnt)
    If Last = " " Then
    Cnt = cnt + 1
    End If
    Next
    F = Name.Chars(0)
    listbox1.items.add(F.ToString.ToUpper & Last.ToString.ToUpper)
    End Sub
    End Class

    Alright so basically i created variables Names, Last, and F. And Cnt for a counter that equals 0. Set Name to textbox1.text, which i will enter the name. And Last is equal to Name.Chars(Cnt) (So last is queal to textbox1 (name) and character cnt, which is 0. Then i created a for next loop, to which Cnt = 0 to the length of the name. - 1 because ... EX) TOM JONES, is 0,1,2,3,4,5,6,7,8. The Length is of TOM JONES is 9(w/o the 0), so you have to subtract - 1. OK, so i set up a If, then. If Last = " " (BLANK) then the counter = counter + 1... So F is then equal to Char 0, which is the first letter of the name, and add everything to a listbox..

    So if i put in a name, Tom Jones for example, it shows TS, The last and first letter, EX) Bob Jones, will display BS.. When it is supposed to be BJ.

    Can anyone help me? I been trying to this for the past couple of hours and have no idea what i am doing wrong.

    Thanks for your time!!!!!!!!!!!!

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,390

    Re: HELP with strings and fornext loop please!!

    Why oh why are you using a listbox to display one result? Ok first get rid of the variable Cnt, the for loop takes care of that. You are making this so hard on yourself. There is no need to be looping chars. For this basic task have a look at string.split

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: HELP with strings and fornext loop please!!

    My teacher is making us use a for next loop

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    Re: HELP with strings and fornext loop please!!

    Another way to go would be to find the space in the name, like this....
    Code:
    For Cnt = 0 To (Name.Length) - 1
        ' find space in name
        If Name.Chars(Cnt) = " " Then
            ' get char after the space
            Last = Name.Chars(Cnt + 1)
            ' last found so exit loop
            Exit For
        End If
    Next
    
    F = Name.Chars(0)
    ListBox1.Items.Add(F.ToUpper & Last.ToUpper)

  5. #5
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: HELP with strings and fornext loop please!!

    There are a other string functions you could use that would be much easier: Left, Right, Mid, Split, InStr and InStrRev come to mind. You should review all String functions and know them as there invaluable when working with strings.

    Now for your teachers requirement of a loop. Do you understand Edgemeal's post(code)? If not then ask about what you don't understand compared to your code. hint: it has to do with how your looping the string.

    Oh yea, Please use 'Code' Tag around your code next time. It is the hash tag instead of quotes.
    Life is about making some things happen, not waiting around for something to happen.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: HELP with strings and fornext loop please!!

    Thanks so much EdgeMeal!!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jan 2015
    Posts
    25

    Re: HELP with strings and fornext loop please!!

    Yah i understand what i was doing wrong!! Thanks

  8. #8
    Fanatic Member
    Join Date
    Aug 2005
    Posts
    606

    Re: HELP with strings and fornext loop please!!

    Cool
    If your question is resolved then please mark it as resolved in the thread tools. This helps others so the don't spend time reading threads, trying to help when it's already been resolved.
    Life is about making some things happen, not waiting around for something to happen.

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