Click here to Skip to main content
13,550,950 members
Rate this:
 
Please Sign up or sign in to 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 18-Oct-17 1:37am
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
011111100010 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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You can use Int32.TryParse function and a minor tweak to get the desire result. See below.
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[^]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Because you are trying to read the input twice. Your code should be something like this:
Try
    Years = Console.ReadLine()
    Console.WriteLine()
    ' check for valid number etc. ...

Catch Exception ' of some type
    Console.WriteLine("Enter a valid number")
End Try
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.180515.1 | Last Updated 18 Oct 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100