Question: Subject: C# .NET How to write code to identify and display the total number of invalid number values outside the 1 to 100 range, and

Subject: C# .NET

How to write code to identify and display the total number of invalid number values outside the 1 to 100 range, and the total number of invalid non-numerical values from the code below. For example:

Invalid Number Values: 3

Invalid Non-Numerical Values: 1

C# Program:

/* C# program that reads input from user and separates both valid and invalid inputs */

using System; using System.Collections.Generic;

namespace InputValidation { class validation { //Main method static void Main() { //List to hold valid inputs List validIps = new List(); //List to hold In-valid inputs List invalidIps = new List(); String value; int temp; //Reading a value from user Console.Write(" Enter input (Press enter to stop) : "); value = Console.ReadLine(); //Read values until user enters presses enter with out any value while(value.Length != 0) { //Checking value for integer if(int.TryParse(value, out temp)) { //Checking for valid range if(temp >= 0 && temp <= 100) //Storing number in valid list validIps.Add(temp); else //Storing number in invalid list invalidIps.Add(value); } else { //If not integer add to invalid list invalidIps.Add(value); } //Reading value again Console.Write(" Enter input (Press enter to stop) : "); value = Console.ReadLine(); } Console.Write(" Valid Inputs: "); // Looping over elements foreach (int val in validIps) { Console.Write(" \t " + val); } Console.Write(" In-Valid Inputs: "); // Looping over elements foreach (object val in invalidIps) { Console.Write(" \t " + val); } //Holding console Console.WriteLine(" Press any key to exit. "); Console.ReadKey(); } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!