How to prevent certain forms of input when writing methods?

I'm still new to programming in high-level programming languages, so I don't know if this is a simple solution, but I'm still happy to learn. I have programmed a small emergency program in C #, which allows the user to enter an alarm within a few seconds, which should go out. It works fine, but the input the user must give must be a number. When the user enters any form of text, the program crashes. Now, how can I prevent users from entering text and calling a function, or doing something else when the user does this, and not just a program crash?

This is the code I have now:

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { class Alarm { public static void play() { int sec; sec = Convert.ToInt16(Console.ReadLine()); for (int i = 0; i < seconds; ++i) { System.Threading.Thread.Sleep(1000); } for (int i = 0; i < 10; i++) { Console.Beep(); } } } } 
+6
source share
8 answers

You must complete the check before converting:

 int sec; if (int.TryParse(Console.ReadLine(), out sec) { // this is valid continue } else { // show error highlighting that entry must be a number } 

int.TryParse will return a boolean value indicating whether the input is a parsiber for int. It will also set your sec variable to if successful.

+11
source

You can loop for an indefinite period until the user enters a number:

 int number = 0; while(!Int32.TryParse(Console.ReadLine(), out number)) { Console.WriteLine("Please input a number."); } 

Int32.TryParse returns false if the conversion failed, instead of throwing an exception and if it successfully returns the result in the second out parameter.

+7
source

You should use the try analysis method.

Something like that:

  int number; bool result = Int32.TryParse(Console.ReadLine(), out number); 

If the result is true, he successfully parsed it, which means it is an integer. If not, this did not work, which means it is not an integer.

Then you can use the number as the value that was parsed as your int32.

+1
source

Already here people answered. I like to do this as an extension method so that I can name it in many places.

 public static bool IsNumeric(this string theValue) { long retNum; return long.TryParse(theValue, System.Globalization.NumberStyles.Integer, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum); } 

Then I'll call it like that

 if(Console.ReadLine().IsNumeric()) { //The value is numeric. You can use it } 
+1
source

replace

 sec = Convert.ToInt16(Console.ReadLine()); 

from

 try { sec = Convert.ToInt16(Console.ReadLine()); } catch(Exception e ){ Console.Writeline(" Enter numbers only"); } 
0
source

What you are trying to do is often called Input Validation . In your case, you need to check that they enter the number before parsing it. Int16 has a TryParse function that will help you.

0
source

Adding a separate function that uses the try statement to catch the error that occurs when trying to hide an invalid string with an integer.

 int readInput() { int sec; while(true) { try { sec = Convert.ToInt16(Console.ReadLine()); return sec; } catch(Exception e) { Console.WriteLine("Enter an integer!"); } } return 0; } 
0
source

If you want to do something more elegant than displaying an error message and ask the user to re-enter the data, you can try to process each keystroke, as happens with Console.Readkey () and reject them if the result is a line, which is not a number.

I did something like this years ago in TurboPascal.

This is pseudo code written from memory, but should lead you to the right path. If you need to accept floating point values, as well as integers or both negative and positive numbers, you need to add more logic to handle these cases.

 string enteredText = ""; char key; bool done = false; while (!done) { key = Console.ReadKey(); if (key is number) enteredText += key; else if (key is backspace) { //remove the last char from enteredText. Handle case where enteredText has length 0 Console.Write(backspace); } else if ((key is enter) && (enteredText.Length > 0)) done = true; else { // invalid char. //MSDN says the char is echoed to the console so remove it Console.Write(backspace); //Beep at the user? } } 
0
source

Source: https://habr.com/ru/post/919131/


All Articles