Eric
Eric

Reputation: 1

Why is Visual Basics Console.ReadLine Buggy and can I fix it?

Hey question about a visual basics program, I am using Console.ReadLine into a string but it seems when I get to that part of the program or any part of my console program the keyboard enter key enters twice or something it completly skips a ReadLine and recieves the input.length 0 before I even have a chance to enter a string.

This isnt runable without my full code I dont think but I cant seem to get ReadLine to prompt for a string when it is initially run, it always returns 0 once and then loops normally. Im wondering if that has something to do with my keyboard or keyboard settings for a visual basics program or command line.

I dont think its a keyboard error because when I am entering code into visual basics it works fine and doesnt send two returns. I couldnt get the full code in code blocks.

...

Shared Sub Main()
    Dim s As New Space()
    Dim Ship As New Ships()
    Dim Run As Boolean = True
    Dim ChooseName As Boolean = True
    Dim ch As Char
    Dim PlayerName As String

    While Run = True
        s.PrintWelcome()
        ch = Convert.ToChar(Console.Read())
        If ch = "1" Then
            While ChooseName = True
                Console.WriteLine("Choose a Name Less then 50 Characters: ")
                PlayerName = Console.ReadLine()
                If PlayerName.Length > 50 Then
                    Console.WriteLine("Name is to Long!")
                ElseIf PlayerName = "Q" Then
                    Run = False
                    Exit While
                ElseIf PlayerName.Length <= 0 Then
                    Console.WriteLine("Please Enter a Player name or Q by itself to quit.")
                Else
                    ChooseName = False
                    Console.WriteLine("PlayerName: " & PlayerName & " PlayerName.Length: {0}", PlayerName.Length)
                End If
            End While
            If Run = True Then

            End If
        ElseIf ch = "2" Then
        ElseIf ch = "3" Then
            Run = False
        ElseIf ch = "Q" Then
            Run = False
        Else
            s.PrintWelcome()
        End If
    End While
End Sub

...

Upvotes: 0

Views: 341

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54487

I suspect that you really ought to be structuring your code more like this:

Module Module1

    Sub Main()
        Do
            Console.Write("Please select a menu item from 1, 2, 3 or Q to quit: ")

            Dim menuSelection = Console.ReadKey()
            Dim input As String = Nothing

            Console.WriteLine()

            Select Case menuSelection.KeyChar
                Case "1"c
                    Console.WriteLine("What did you want to say about 1?")
                    input = Console.ReadLine()
                Case "2"c
                    Console.WriteLine("What did you want to say about 2?")
                    input = Console.ReadLine()
                Case "3"c
                    Console.WriteLine("What did you want to say about 3?")
                    input = Console.ReadLine()
                Case "q"c, "Q"c
                    Exit Do
                Case Else
                    'Do nothing before repeating the prompt.
            End Select

            If Not String.IsNullOrEmpty(input) Then
                Console.WriteLine("You said: " & input)
            End If
        Loop
    End Sub

End Module

The ReadKey call will immediately read the first key entered by the user. Rather than requiring the user to hit Enter after that key, the application writes the line break itself. It then tests the chararacter represented by that key to see if it is a valid menu item. If it is, it does whatever is appropriate for that item, whether that be reading a line of input or quitting. If the key does not represent a valid menu item then it simply loops back to the prompt. Note also that the Char value entered by the user is actually compared to Char literals, the way it should be done, rather than String literals.

Upvotes: 1

Related Questions