Input Snag

I need to know if lines #7 and #8 can be put together somehow. I have a filling they are the reason I have to press enter twice after entering my first and last name. Also why is my period spaced away from the end of my sentence.
Module Module1

    Sub Main()
        Dim a As String, b As String

        Console.Write("Enter user first and last name, and then press enter: ")
        a = Console.ReadLine()
        b = Console.ReadLine()
        Console.WriteLine("Hello " & a & " " & b & ".")
    End Sub

End Module ' My Input program.

Comments

  • I suppose you could enter both first and last names into the same string, then split the string at the space.

    BTW: here is a tutorial I found very useful. and I still refer to it occasionally. If you buy the ebook you get a few more chapters.
  • You mean like this. Well that sure is nifty that it can do that so easily. If that is not what you meant I could care less it does what I wanted it to do. As for the book I will open up the first chapter tomorrow.
    Module Module1
    
        Sub Main()
            Dim a As String
    
            Console.Write("Enter user first and last name, and then press enter: ")
            a = Console.ReadLine()
            Console.WriteLine("Hello " & a & ".")
        End Sub
    
    End Module ' My Input program.
    
  • I had this in mind where you split the sentence into individual words.
        Sub Main()
            Dim a As String
    
            Console.Write("Enter user first and last name, and then press enter: ")
            a = Console.ReadLine()
            Dim words As String() = a.Split(New Char() {" "c})
            Console.Write("Hello ")
            Dim word As String
            For Each word In words
                Console.Write(word & " ")
            Next
            Console.Write(vbNewLine)
        End Sub
    
  • I never would have guessed that, and it does not display a period at the end of the sentence. I also do not know how to get two integers input instead of one at a time. For instance it would end up being a + b instead of a + a.
    Module Module1
    
        Sub Main()
            Console.Write("Enter two integers, and then press enter: ")
            Dim a As Double = Console.ReadLine()
            Console.WriteLine(a & " + " & a & " = " & a + a)
        End Sub
    
    End Module ' Main.vb
    
  • I have found that the lessons in the book you posted a link too do not teach about console application programming.
  • That's probably because VB.NET really shines at GUI applications. It has only minimal support for console.
  • That's probably because VB.NET really shines at GUI applications. It has only minimal support for console.

    So I have noticed it could use a little work. :icon_eek: I mean I am either doing this wrong, or it just is not meant to be none at all. I don't know you tell me.
  • Use Console.Read() instead of Console.ReadLine()
            Console.Write("Enter two integers, and then press enter: ")
            Dim a As Integer = Console.Read()
            Dim b As Integer = Console.Read()
            Console.WriteLine(a & " + " & b & " = " & a + b)
    
  • Get input as string, split the string as in my previous example, then convert each part to integer
            Console.Write("Enter two integers, and then press enter: ")
            Dim a As String = Console.ReadLine()
            Dim words As String() = a.Split(New Char() {" "c})
            Dim c As Integer = Convert.ToInt32(words(0))
            Dim d As Integer = Convert.ToInt32(words(1))
    
            Console.WriteLine(c & " + " & d & " = " & c + d)
    
  • Ok I like the way your input for post #9 works, but in post #3 it make more sense at least for me anyways to do it that way. Is there any reason what so ever why I should not use my code in post #3 when doing strings as simple as that one?
  • Certainly, use the code in your post #3 if all you want to do is print the string that you typed. But you have to do a little more if you want to enter 2 integers then add them together.
Sign In or Register to comment.