Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need this to validate an input from the user. It needs to ensure that it is numeric and that it is in the range of -1 - 5 including 0 as a valid input.

What I have tried:

Sub Main()
       Dim Years As Integer
       Dim Valid As Boolean = False

       Do Until Valid = True
           Console.WriteLine("Enter a number between -1 and  5")

           If IsNumeric(Console.ReadLine) Then
               Years = Console.ReadLine()
               Console.WriteLine()
               Select Case Years
                   Case -1 To 5
                       Valid = True
                   Case Else
                       Console.WriteLine("The number is outside the range")

               End Select
               Console.WriteLine("Enter a valid number")
           End If


       Loop
       Console.ReadLine()
   End Sub
Posted
Updated 18-Oct-17 4:41am
Comments
Richard MacCutchan 18-Oct-17 7:49am    
What is the question?
Teal_ 18-Oct-17 7:56am    
currently the code posted will catch if a alpha numeric is entered but if a number outside or inside the range is entered it will throw the error on the line "Years = Console.Readline" "Conversion from string "" to type 'Integer' is not valid". I don't know why this is happening. Additionally when i remove the "if IsNumeric(console.readline) Then " it works. I would like help fixing it so that it will only exit the loop if a number between -1 and 5 is entered
ZurdoDev 18-Oct-17 8:03am    
Console.ReadLine take input from the user. So, don't call it twice. Store it in a string and then check it.

"Conversion from string "" to type 'Integer' is not valid". - Look at your code. You defined Years as an Integer so when you type in something that is not integer, you'll get that error.
Peter_in_2780 18-Oct-17 8:00am    
Your problem is calling Console.ReadLine twice. You need to call it once, and use that result to test then convert.

You can use Int32.TryParse function and a minor tweak to get the desire result. See below.
VB
Dim Years As Integer
        Dim Valid As Boolean = False

        Console.WriteLine("Enter a number between -1 and  5")

        Do Until Valid = True
            If (Int32.TryParse(Console.ReadLine(), Years)) Then
                Select Case Years
                    Case -1 To 5
                        Valid = True
                        Console.WriteLine("Good job!!!")
                    Case Else
                        Console.WriteLine("The number is outside the range")
                End Select
            Else
                Console.WriteLine("Enter a valid number")
            End If

        Loop
        Console.ReadLine()

Here the link to test it out VB Console Integer Range[^]
 
Share this answer
 
v2
Because you are trying to read the input twice. Your code should be something like this:
VB
Try
    Years = Console.ReadLine()
    Console.WriteLine()
    ' check for valid number etc. ...

Catch Exception ' of some type
    Console.WriteLine("Enter a valid number")
End Try
 
Share this answer
 
Comments
Ralf Meier 18-Oct-17 9:13am    
Years is defined as integer.
Console.Readline will bring you a string - so I suggest to make a converting of this to Integer ...
Richard MacCutchan 18-Oct-17 10:53am    
Thank you. I should have seen that, but I am very much a novice at VB.NET.
Ralf Meier 19-Oct-17 2:12am    
I don't believe you ... ;) ... but no matter ... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900