C# Program: Numeric and integer input validation
Write a C# program that prompts the user to input a numeric integer and throws an exception if the number is less than 0 or greater than 1000.
Sample Solution:
C# Sharp Code:
using System;
class Program {
static void Main() {
try {
// Prompt user to input a numeric integer and read the input
Console.Write("Input a numeric integer: ");
int number = Convert.ToInt32(Console.ReadLine());
// Call method to validate number range
ValidateNumberRange(number);
// Display valid input if no exception was thrown
Console.WriteLine("Valid input: " + number);
} catch (NumberOutOfRangeException ex) {
// Catch block for handling NumberOutOfRangeException
Console.WriteLine("Error: " + ex.Message);
} catch (FormatException) {
// Catch block for handling FormatException when non-numeric input is entered
Console.WriteLine("Error: Invalid input. Please enter a numeric integer.");
} catch (Exception ex) {
// Catch block for handling other types of exceptions
Console.WriteLine("An error occurred: " + ex.Message);
}
}
// Method to validate if the number is within the specified range
static void ValidateNumberRange(int number) {
if (number < 0 || number > 1000) {
// Throw NumberOutOfRangeException if the number is out of the specified range
throw new NumberOutOfRangeException("Number out of range. Please enter a number between 0 and 1000.");
}
}
}
// Custom exception class for NumberOutOfRangeException
class NumberOutOfRangeException: Exception {
public NumberOutOfRangeException(string message): base(message) {}
}
Sample Output:
Input a numeric integer: -1 Error: Number out of range. Please enter a number between 0 and 1000.
Input a numeric integer: 1001 Error: Number out of range. Please enter a number between 0 and 1000.
Input a numeric integer: 0 Valid input: 0
Input a numeric integer: 1000 Valid input: 1000
Input a numeric integer: 666 Valid input: 666
Explanation:
In the above exercise,
- The Main() method prompts the user to input a numeric integer using Console.Write() and Console.ReadLine(). The input is converted to an int using Convert.ToInt32() and stored in the variable number.
- The ValidateNumberRange method is called with the number as an argument. This method checks if the number is less than 0 or greater than 1000. If it is, it throws a custom exception called NumberOutOfRangeException with the message "Number out of range. Please enter a number between 0 and 1000."
- In the Main method, the exceptions are handled using catch blocks. If a NumberOutOfRangeException is thrown by the ValidateNumberRange method, it is caught and the error message is displayed.
- If a FormatException occurs, the user entered a non-integer value. This exception is caught and an appropriate error message is displayed.
- Any other exceptions are caught by the generic catch block, and a general error message is displayed.
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: File opening with exception handling.
Next: Calculate the average of integers in an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics