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

  • SEARCH
  • Home
  • 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 115203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:58:55+00:00 2026-05-11T02:58:55+00:00

Oh, 2 things: 1) It is a console application. 2 ) I know it

  • 0

Oh, 2 things: 1) It is a console application. 2 ) I know it is in danish, but it doesn’t really matter, its just an example of asking for some input. The text and variables does not matter.

Alright, consider this simple input: It could be any sort of input question really.

Console.WriteLine('Hvad er dit kundenummer: (Kun hele tal tilladt)'); string inputKnr = Console.ReadLine(); kundenummer = Convert.ToInt16(inputKnr); 

Now, what if the customer types something wrong? Such as a letter. A try & catch would make sure the application does not break, but that is not the solution I want. I want it to say that you did it wrong, try again. Pretty classic right?

But what is the best way to solve this solution? I have thought of this:

bool fangetKundenummer = true; while (fangetKundenummer) { Console.WriteLine('Hvad er dit kundenummer: (Kun hele tal tilladt)'); string inputKnr = Console.ReadLine(); try {     kundenummer = Convert.ToInt16(inputKnr);     fangetKundenummer = false; } catch {     Console.WriteLine('Fejl. Prøv igen'); } } 

But it just doesn’t seem like the right way to do it.

Also, just to mention it, this little application I am playing with has 4 input questions in a row. This would mean 4 times this nasty while() loop.

You could also write a function. Something like this (no reason to do it the right way, its just to illustrate a concept):

static void verifyInput() {     try     {         Console.WriteLine('question');         input = Console.ReadLine();         kundenummer = Convert.ToInt16(input)     }     catch     {         Console.WriteLine('Wrong. Do it over');         verifyInput(); //start the function all over     } } 

But you’d have to write a function for each and every input question, even though they might ask exactly for the same! (meaning perhaps all asking for an integer; but with a different question and variable).

This doesn’t seem much better than the while() solution.

Does anyone have a clever idea?

  • 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. 2026-05-11T02:58:56+00:00Added an answer on May 11, 2026 at 2:58 am

    Use Int16.TryParse and the equivalents for other numeric types. All of these return a Boolean result to indicate success or failure for parsing, and take an out parameter which is set to the result of the parsing (or 0 in case of failure). In your case you may want to wrap the call in a method to keep prompting:

    static Int16 PromptForInt16(string prompt) {     while (true)     {         Console.Write(prompt);         Int16 result;         if (Int16.TryParse(Console.ReadLine(), out result))         {             return result;         }         Console.WriteLine('Sorry, invalid number entered. Try again.');     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 91k
  • Answers 91k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll need to use a client-side timer (or some other… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer function obj_array_map($method, $arr_of_objects) { $out = array(); $args = array_slice(func_get_args(),… May 11, 2026 at 6:13 pm
  • Editorial Team
    Editorial Team added an answer UPDATE is by far much faster. When you UPDATE, the… May 11, 2026 at 6:13 pm

Related Questions

Looking for some direction here as I'm running into some migration problems. We have
Hopefully a picture is worth a thousand lines of code because I don't want
I have a Monthly Status database view I need to build a report based
I've become interested in algorithms lately, and the fibonacci sequence grabbed my attention due

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.