Int input from Console

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adrián E. Córdoba

    Int input from Console

    Sorry for very silly question.
    But, how can I input an integer from keyboard in C# Console
    applications?
    Like this, in C++ code:
    "int i ; cin >i ;"
    I just read some tutorials about C#, and I can get a "string" from
    keyboard, but I can't get an integer.

    Thank you in advance.

  • Arne Vajhøj

    #2
    Re: Int input from Console

    Adrián E. Córdoba wrote:
    But, how can I input an integer from keyboard in C# Console
    applications?
    Like this, in C++ code:
    "int i ; cin >i ;"
    I just read some tutorials about C#, and I can get a "string" from
    keyboard, but I can't get an integer.
    int v = int.Parse(Conso le.ReadLine());

    was at least one way.

    Arne

    Comment

    • Adrián E. Córdoba

      #3
      Re: Int input from Console

      Arne:
      Thank you very much.

      Arne Vajhøj wrote:
      Adrián E. Córdoba wrote:
      But, how can I input an integer from keyboard in C# Console
      applications?
      Like this, in C++ code:
      "int i ; cin >i ;"
      I just read some tutorials about C#, and I can get a "string" from
      keyboard, but I can't get an integer.
      >
      int v = int.Parse(Conso le.ReadLine());

      was at least one way.

      Arne

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Int input from Console

        Hi,

        quick , dirty code :)

        int i=0;
        while( true )
        {
        try {
        i = Convert.ToInt32 ( Console.ReadLin e() );
        break;
        }catch{
        Console.WriteLi ne( "Invalid number");
        }
        }



        --
        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        "Adrián E. Córdoba" <software.asia@ gmail.comwrote in message
        news:1158175223 .776248.46970@e 3g2000cwe.googl egroups.com...
        Sorry for very silly question.
        But, how can I input an integer from keyboard in C# Console
        applications?
        Like this, in C++ code:
        "int i ; cin >i ;"
        I just read some tutorials about C#, and I can get a "string" from
        keyboard, but I can't get an integer.
        >
        Thank you in advance.
        >

        Comment

        • Ignacio Machin \( .NET/ C# MVP \)

          #5
          Re: Int input from Console

          Hi,



          "Arne Vajhøj" <arne@vajhoej.d kwrote in message
          news:WfZNg.3782 2$_q4.2339@duke read09...
          Adrián E. Córdoba wrote:
          >But, how can I input an integer from keyboard in C# Console
          >applications ?
          >Like this, in C++ code:
          >"int i ; cin >i ;"
          >I just read some tutorials about C#, and I can get a "string" from
          >keyboard, but I can't get an integer.
          >
          int v = int.Parse(Conso le.ReadLine());
          You may get an exception if what you read is not a number


          --
          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          Comment

          • Arne Vajhøj

            #6
            Re: Int input from Console

            Ignacio Machin ( .NET/ C# MVP ) wrote:
            "Arne Vajhøj" <arne@vajhoej.d kwrote in message
            >int v = int.Parse(Conso le.ReadLine());
            >
            You may get an exception if what you read is not a number
            Yep.

            The original poster should catch the exception and
            so something appropriate if that happen.

            Arne

            Comment

            • Adrián E. Córdoba

              #7
              Re: Int input from Console

              For your information:
              I found in a tutorial, a compact expression for Integer input:

              int i = Convert.ToInt32 (Console.ReadLi ne());

              Thank you again.
              Best regards.

              --
              Adrián

              Comment

              • Arne Vajhøj

                #8
                Re: Int input from Console

                Adrián E. Córdoba wrote:
                For your information:
                I found in a tutorial, a compact expression for Integer input:
                >
                int i = Convert.ToInt32 (Console.ReadLi ne());
                To quote from docs for Convert.ToInt32 :

                Remarks
                The return value is the result of invoking the Int32.Parse method on value.

                Arne

                Comment

                Working...