Open E-School

Male
Friday, February 24, 1984

C# - How to read integer input from user in Console program

Category: Programming Date: Wednesday, March 25, 2015

Goal

Learn how to read an integer input from user in a C# console application.

Namespaces used

System – Console class is defined in the system namespace

Classes used

  1. Console – Contains all console input output related methods and properties
  2. Convert – Contains all base type conversion methods

Methods used

  1. Console.ReadLine() – Static method defined in the Console class
  2. Conver.ToInt() – Static method defined in the Convert class.

Brief description

When reading input from the user from Console you will be using Console.ReadLine() method. The problem here is that the Console.ReadLine() method returns the input in the form of a string no matter what you type from the keyboard. In order to read an integer value using the console application you need to convert the input you get from Console.ReadLine() into an integer using the Conver.ToInt32() method.

Code

using System;

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            Console.Write("Enter first number : ");
            string userInput1 = Console.ReadLine();
            Console.Write("Enter second number : ");
            string userInput2 = Console.ReadLine();
            int number1 = Convert.ToInt32(userInput1);
            int number2 = Convert.ToInt32(userInput2);
            int sum = number1 + number2;
            Console.WriteLine();
            Console.WriteLine("The sum of {0} and {1} is {2}", number1, number2, sum);
        }
    }
}

Output

C# - How to read integer input from user in Console program

 
Explanation of the code

In the code example above we are asking the user to enter two numbers. We are reading the two numbers entered by the user in string variables userInput1 and userInput2. But our goal is to add the two numbers and we cannot add two numbers in the string format. So next in the code we are converting userInput1 and userInput2 to integer values by using the Convert.ToInt32(userInput1) and Conver.ToInt32(userInput2) methods.
The Convert class has methods to convert one base type into another base type. Here we are converting a string into an int. After this conversion we are adding the two numbers and then printing their sum.

Extra Remarks

If the user enters a non-numeric input from the keyboard then the Convert.ToInt32() method will throw a FormatException error saying that the input string was not in the correct format.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp