get a number from console?

DOTNETCoder33

Member
Joined
Jun 18, 2010
Messages
15
Programming Experience
Beginner
all i need to know is how to stop the user from entering letters rather than numbers in this example:

Module Module1

Sub Main()
Dim test As Integer

Console.Write("Please enter a number: ")
test = Console.ReadLine

End Sub

End Module





i have heard i could use "ISNumeric" but i put it in a "Do Loop Until" but still didnt work:

Module Module1

Sub Main()
Dim test As Integer

Do

Console.Write("Please enter a number: ")
test = Console.ReadLine

Loop Until IsNumeric(test)


End Sub

End Module



just get a runtime error !! arghh!


how would i fix this? thanks, and vice-versa ... how would i get them to enter only text rather than numbers?




:D
 
VB.NET:
Module Module1

Sub Main()
Dim test As Integer

Console.Write("Please enter a number: ")
If IsNumeric(Console.ReadLine) Then
    test = Console.ReadLine
Else
    Console.Write("You must supply a valid number")
End If

End Sub

End Module
 
hey

thanks for the reply. unfortunately this still doesnt work, i put in the number "44" for exmaple and it gave me a runtime error ... :S


i tried this and it seems to work but now the problem is that the user can only have ONE shot at it before the program will close

Module Module1

Sub Main()
Dim test As Integer

Try
Console.Write("Please enter a number: ")
test = Console.ReadLine
Catch
Console.WriteLine("error - must be a number")

End Try
End Sub

End Module





i was going to put in a Do Loop-Until but i cant create a boolean term on this as i still dont know how to specify that the users input MUST be numeric.



anyone? ... still stuck on this.
 
try this number getter:
VB.NET:
Function GetNumber() As Integer
    Do
        Console.Clear()
        Console.Write("enter a number: ")
        Dim num As Integer
        If Integer.TryParse(Console.ReadLine, num) Then
            Return num
        End If
    Loop
End Function
VB.NET:
Dim num As Integer = GetNumber()
 
Use the TryParse() method, which returns a Boolean value. Here is a simple example which works with a type Double number. If you need an integer instead, just substitute Integer in place of Double for both the declaration and the method. If needed, you can also add more code to specify the maximum and minumum value of the required number as a condition.

VB.NET:
	Sub Main()
		Dim num As Double, snum As String, ok As Boolean
		Do
			Console.Write("Please enter a valid number:  ")
			snum = Console.ReadLine
			ok = Double.TryParse(snum, num)
			If ok = False Then
				Console.WriteLine("Wrong input.  Try again.  ")
				Console.WriteLine()
			End If
		Loop While ok = False
		Console.WriteLine("OK")
		Console.ReadLine()
	End Sub


(Note: JohnH posted a similar function above using the TryParse method. I added a separate Boolean variable for clarification in my example.)
 
hey guys, well after long nights lol i think i have found the perfect example i coded myself to get the job done and its alot simpler as well


Module Module1

Sub Main()
Dim User_Input As String
Dim Number As Single

Do
Console.Write("Enter a Number: ")
User_Input = Console.ReadLine

Loop Until IsNumeric(User_Input)
Number = CSng(User_Input)
Console.WriteLine(" test {0}", Number)

End Sub

End Module





using the CSng method i can convert it to a single data type and will keep looping until its Numeric ... :D


have i done well?? ... lol
 
tried this also with a number like -33 to try and catch it out and this works fine as well as i thought the IsNumberic would fail at the -33 as it had the Minus in there ... all confusing as im still new but think ive done alright??
 
ok been playing around with it and this works equally as well:

Module Module1

Sub Main()
Dim User_Input As String

Do
Console.Write("Enter a Number: ")
User_Input = Console.ReadLine
Loop Until IsNumeric(User_Input)
Console.WriteLine(User_Input)

End Sub
End Module




works for even negative numbers, easier than i thought :S
seems almost to easy to be true after looking at all the above exmaples, they have a higher detail of complexity to the one i have done but returns the results im looking for.
are there any flaws here?
 
You did well enough, using legacy VB6 functions. You should be using the latest .NET methods instead.

IsNumeric() is a legacy VB6 function that returns a type Double.
The TryParse() method is a newer .NET method that converts a string into any type number, all in a single statement. For example:

Single.TryParse(User_Input, Number)

---------------------------------------------
Exactly what kind of number do you require? Do you need a real number (with a decimal point) or a whole number? In most cases, a type Double is used rather than Single for a real number, or a type Integer for a whole number.

Is there a range of numbers that the user should not enter less than a minimum or more than a maximum?

The following example shows how to code for that scenario. Substitute any numbers you require for the min and max (including any negative values):

VB.NET:
	Sub Main()
		Dim num As Integer, snum As String
		Dim min As Integer = 50
		Dim max As Integer = 60
		Do
			Console.Write("Enter a number between " & min & " and " & max & ":  ")
			snum = Console.ReadLine
			Integer.TryParse(snum, num)
			If num < min Or num > max Then
				Console.WriteLine("Wrong input.  Try again.  ")
				Console.WriteLine()
			End If
		Loop While num < min Or num > max
		Console.WriteLine("Good")
		Console.ReadLine()
	End Sub

----------------------------------------------------

Here is how the TryParse() method works:

Dim mystring As String
Dim mynumber As Integer
mystring = Console.ReadLine()
Integer.TryParse(mystring, mynumber)

Integer is the Class. TryParse is the method. It includes two arguments. The first argument is the string to convert. The second argument is the number (of the same type as the Class) to convert into.

The TryParse() method returns a Boolean value. If the string cannot be converted into the number of the correct type, it returns a 0 without generating an error. Otherwise, it does the conversion and returns the converted number.

You could also do this:

Dim ok As Boolean
ok = TryParse(mystring, mynumber)
If ok = True Then
Console.WriteLine("Good. Number is {0}", mynumber)
Else
Console.WriteLine("Wrong")
End If
 
Last edited:
thanks for the reply and taking the time to write that out for me. i understand that a bit better now :) im still happy with the IsNumeric at this stage, but im sure i will see more sense later in learning about the TryParse().

im also studying the .NET 2.0 books ... should i be worried as the new 3.5/ 4.0 is out? ... im not wasting my time? ... spent so long at the 2.0 stuff would be a shame to find ive wasted it all?



thanks
 
.NET 2.0 is really old - it came out with Visual Studio 2005. .NET 3.5 came out with Visual Studio 2008. A lot of new stuff was added with each new version of the .NET framework. But everything in 2005 will work in 2008, and everything in 2008 will work in 2010, which uses .NET 4. So you have nothing to worry about.
 
.NET 2.0 is really old - it came out with Visual Studio 2005. .NET 3.5 came out with Visual Studio 2008. A lot of new stuff was added with each new version of the .NET framework. But everything in 2005 will work in 2008, and everything in 2008 will work in 2010, which uses .NET 4. So you have nothing to worry about.


good stuff :D
 
Back
Top