Home > OS >  C# input integer and char one line
C# input integer and char one line

Time:11-12

input integer and char in one line. This format is (5s)

int number = Convert.ToInt32(Console.ReadLine());

for(int i=0; i<number; i  )
{
    int number ,char n = Convert.ToInt32(Console.ReadLine());                                   
}

CodePudding user response:

for(int i=0; i<number; i  ){
        string entry;
        entry = Console.ReadLine();
        int num;
        if (int.TryParse(entry, out num))
           {
             // it's a integer
            }
        else{
             // it's a string
            }
      }

I am not sure what is your actual intent of doing this. But what you can do is, read the input and try to parse it to Integer. If it is parsed then it is integer and you can use it the way you intent to. If it is not parsed then that is probably a string and that's how you can differentiate.

CodePudding user response:

You can get it as a string and then substring and parse to the two variables

string input = Console.ReadLine();
int number = int.Parse(input.Substring(0,1));

for(int i=0; i<number; i  )
{
    string input = Console.ReadLine();
    int number = int.Parse(input.Substring(0,1));
    char n = char.Parse(input.Substring(1));                                   
}
  • Related