Demonstrate the IsAllDigits method. - CSharp Language Basics

CSharp examples for Language Basics:char

Description

Demonstrate the IsAllDigits method.

Demo Code

using System;/*from  w  w w .  j a va2 s . c om*/
class Program
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Enter an integer number");
      string s = Console.ReadLine();
      if (!IsAllDigits(s)) // Call our special method.
      {
         Console.WriteLine("Hey! That isn't a number");
      }
      else
      {
         int n = Int32.Parse(s);
         Console.WriteLine("2 * " + n + ", = " + (2 * n));
      }
   }
   public static bool IsAllDigits(string raw)
   {
      string s = raw.Trim();  // Ignore whitespace on either side.
      if (s.Length == 0)
      {
         return false;
      }
      for(int index = 0; index < s.Length; index++)
      {
         if (Char.IsDigit(s[index]) == false)
         {
            return false;
         }
      }
      return true;
   }
}

Result


Related Tutorials