Read multiple line input from c# console application

Dev Skill archived problems can be discussed here.

Re: Read multiple line input from c# console application

by mahrahat » Fri Mar 31, 2017 4:00 pm

Try
Code: Select all
while ((line=Console.ReadLine())!=null && line != "")

instead of
Code: Select all
while ((line = Console.ReadLine()) != string.Empty)
Lets
• search through previous topics before posting a new topic
• include link to problem (or at least problem number)
• write code snippets inside [code] [ /code] tags
 
Posts: 48
Joined: Thu Nov 24, 2016 9:11 pm
Location: Bangladesh

Re: Read multiple line input from c# console application

by mahrahat » Mon Apr 03, 2017 5:43 pm

Here you want to read 2 'sets' of data, right?

First, lets revise how to read one 'set' of data:
1. Read an integer, say, N
2. Read N number of lines, each line contains several numbers.
3. Read a blank line.

Now, when there are several sets of data and we don't know how many sets are there, then we need to put above steps inside a loop. So the code snippet looks like:
Code: Select all
while ((line=Console.ReadLine())!=null && line != "")
{
    int N = int.Parse(line);
    //Read N number of lines
    Console.ReadLine(); //blank line
}


Alternatively:
Code: Select all
while (true)
{
    String line = Console.ReadLine();
    if (line==null || line == "")
        break;
    int N = int.Parse(line);
    //Read N number of lines
    Console.ReadLine(); //blank line
}
Lets
• search through previous topics before posting a new topic
• include link to problem (or at least problem number)
• write code snippets inside [code] [ /code] tags
 
Posts: 48
Joined: Thu Nov 24, 2016 9:11 pm
Location: Bangladesh


Who is online
Users browsing this forum: Bing [Bot] and 1 guest
cron