Read an input from console and convert to integer : int parse « Data Types « C# / C Sharp






Read an input from console and convert to integer

  


using System;
public class MainClass
{
    public static void Main()
    {
        int newInteger = 0;
        try
        {
            newInteger = System.Convert.ToInt32(System.Console.ReadLine());
        }
        catch (System.ArgumentNullException)
        {
            System.Console.WriteLine("String is null.");
        }
        catch (System.FormatException)
        {
            System.Console.WriteLine("FormatException");
        }
        catch (System.OverflowException)
        {
            System.Console.WriteLine("Overflow");
        }

        System.Console.WriteLine("Your integer as a double is {0}", System.Convert.ToDouble(newInteger));
    }
}

   
    
  








Related examples in the same category

1.Converts string to integer
2.Int32.TryParse Method (String, Int32)
3.Parse string to integer