I want to a C# console application code... My problem is... i want take input from keybord a ID like 10510023.. and the 1st 2 digit of this code indicate the year of a student like if ID is 10510023 the year is 2010 or if it is 13510023 the year is 2013.. like same... 3rd digit indicate his or her semester, 4th digit for her branch.. and next digit is serial.... please someone help me...

Edited 3 Years Ago by pritaeas: Moved.

Since you're coding a console app, use console.readline and your input will be a string. from there simply validate that each character is a digit, and a for loop can easily parse the string into the values you want.

here string is using to store ID

string str;
str = Console.ReadLine();
Console.Write("year is=20");
for (int i = 0; i < 2; i++)
Console.Write(str[i]);
Console.WriteLine("semester is=" + str[2]);
Console.WriteLine("Branch  is=" + str[3]);
Console.Write("serial is=");
for (int i = 4; i < str.Length; i++)
Console.Write(str[i]);
Console.ReadKey();

hope this helps you to solve the issue . . .

thanks for reply rishif2.. but i need something more ... if str[2] is 3 i want to show semester is fall if it is 4 i want show summer then if it is 5 i want to show spring... and also want to do the same thing with the branch for different digit i want to show the name of different branch, pls help me..

You could use Enums for instance

enum semester
{
    Fall = 4,
    Summer = 5,
    Spring = 6
};

then

Console.WriteLine("semester is=" + Enum.GetName(typeof(semester), int.Parse(str[2])));

You can do the same with the branches.

Edited 3 Years Ago by tinstaafl

@tinstaafl

int.Parse(str[2])

i think that int.Parse will take string argument and so it will not work since str[2] is char

so , to execute this , we need to convert it to string :-

Console.WriteLine("semester is=" + Enum.GetName(typeof(semester), int.Parse(str[2].ToString())));

This article has been dead for over six months. Start a new discussion instead.