1

My input is a year in 4 digits between 1900-3000. So the input has to be 4 digits 1994, 2022,2950.

However, how do I check if the input is only 3 digits or less or 5 digigts or more? I have alread made an if statement if it's over 1900 or 3000.

   if (YearNumb<1900 || YearNumb>3000)
        {
            Console.WriteLine("Du angav inte ett år mellan 1900 och 3000.");
            Console.ReadLine();
            return;
Essej
  • 892
  • 6
  • 18
  • 33

2 Answers2

4
if (YearNumb<1900 || YearNumb>3000)
        {
            Console.WriteLine("Du angav inte ett år mellan 1900 och 3000.");
            Console.ReadLine();
            return;
        }
else if (YearNumb.ToString().Length != 4)
           {
            Console.WriteLine("text");
            Console.ReadLine();
            return;
        } 

Convert to string and check length.

user1948635
  • 1,357
  • 4
  • 15
  • 22
  • Doesn't work properly, if I enter 1700 it does not execute it. – Essej Feb 10 '14 at 17:15
  • Oops, edited above to solve that issue and the ability to show a different message. – user1948635 Feb 10 '14 at 17:17
  • @Baxtex, there is no way that the second `if` will ever be entered. Its a pointless `ToString()` call followed by a redundant length check. If `YearNumb` is an `int` it cannot be between `1900` and `3000` and have a length other than `4`. Unless, I suppose, the culture you are under has a non decimal representation for numbers. – Jodrell Feb 11 '14 at 09:21
-1

ok,

if (YearNumb < 0)
{
    ...
} 
else if (YearNumb < 1000)
{
    ...
}
else if (YearNumb > 9999)
{
    ...
}
else if (YearNumb < 1900 || YearNumb > 3000)
{
    ...
}

Is probably more like what you are looking for.

Jodrell
  • 34,946
  • 5
  • 87
  • 124