Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:02:42+00:00 2026-06-12T00:02:42+00:00

I am using Integer.TryParse Method to validate whether user input is a numeric or

  • 0

I am using Integer.TryParse Method to validate whether user input is a numeric or non-numeric in my program.
1)if the user input is numeric, the program will then proceed and validate that the user input is range from 0 to 9.
2)If the user is enter a non-numeric input, the program will display the message “invalid input” and ask user to start from beginning.

Following is my coding:
Sub Main()

    Dim sevenNumbers As Integer()
    sevenNumbers = New Integer(6) {}
    Dim index As Integer
    Dim number As Integer
    Dim reEnter As Boolean = True

    Console.WriteLine("Please enter 7 integers: ")
    Console.WriteLine("<ATTENTION: FROM 0 TO 9 ONLY>")
    Console.WriteLine()

    While reEnter
        For index = 0 To 6

            Console.WriteLine("Please enter the integer no." & "{0}" & " : ", index + 1) 'Prompt user to enter 7 integers.
            sevenNumbers(index) = Console.ReadLine() 'The 7 integers are stored in an array.

            If Integer.TryParse(sevenNumbers(index), number) Then
                While sevenNumbers(index) < 0 Or sevenNumbers(index) > 9
                    Console.WriteLine("<invalid input>")
                    Console.WriteLine()
                    Console.WriteLine("------------------------------------------")
                    Console.WriteLine("<Please re-enter the 7 integers>")
                    Console.WriteLine("------------------------------------------")
                    Console.WriteLine()
                    reEnter = True
                    Exit For
                End While

            Else
                Console.WriteLine("<invalid input>")
                Console.WriteLine()
                Console.WriteLine("------------------------------------------")
                Console.WriteLine("<Please re-enter the 7 integers>")
                Console.WriteLine("------------------------------------------")
                Console.WriteLine()
                reEnter = True
                Exit For

            End If

            reEnter = False

        Next
    End While

End Sub

However, when a user enter a non-numeric input, the program can’t continue and shows an error that forced to close.

i tried this

Sub Main()
Dim num As Integer

    Console.Write("enter num:")
    Dim input = Console.ReadLine

    If Integer.TryParse(input, num) Then
        Console.WriteLine("valid. num = " & num)
    Else
        Console.WriteLine("invalid")
    End If
End Sub

it does works and i am wondering which part of my coding is wrong??

Thank for help!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T00:02:43+00:00Added an answer on June 12, 2026 at 12:02 am

    Your two samples of code are different. In your second attempt, you do this:

    Dim input = Console.ReadLine 
    If Integer.TryParse(input, num) Then
    

    The above code reads into a variable called input that will be a String (because Console.ReadLine returns a String). Then, you try to parse the string into a number.

    However, in your original code, you do this (some lines omitted for clarity):

    Dim sevenNumbers As Integer()  
    sevenNumbers = New Integer(6) {}  
    ...
    sevenNumbers(index) = Console.ReadLine() 
    

    In this case, you are reading into a variable that you have explicitly declared to be an Integer. If the user types "abc" then the conversion will fail at this point – you won’t even get to the TryParse because you can’t complete the innput.

    Instead of reading to an integer, you need to read into a String variable and then parse that value into an Integer (as you did in your second code).

    You could have spotted this yourself by taking note of the line that the error actually occurs on when debugging: you should note that the program crashes on the ReadLine, not on the TryParse.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C++ I need to combine two distinct IDs into one 16bit integer. Then
How do you solve the max value problem when using an integer counter, that
I'm writing some code that returns an integer, which then needs to be outputted
dear All. I'm using integer PKs in some tables of mysql database. Before input
I have got this code that reads an integer using scanf and checks if
I need to generate a graph using integer arrays. Edges of the graphs are
We need to represent huge numbers in our application. We're doing this using integer
I need a custom widget height. I tried using this Integer.toString(yourWidget.getElement().getOffsetHeight()) but, If I
I'm parsing a binary file format. It encodes an integer using four bytes in
I am running into integer overflow using the standard ftell and fseek options inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.